Answers:
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'<stepname>', @step_id=1, @cmdexec_success_code=0, @on_success_action=3, @on_success_step_id=0, @on_fail_action=2, @on_fail_step_id=0, @retry_attempts=0, @retry_interval=0, @os_run_priority=0, @subsystem=N'TSQL', @command=N'<code>', @database_name=N'', @flags=0
DECLARE @jobId BINARY(16) SET @jobId = (SELECT job_id FROM msdb.dbo.sysjobs WHERE name = '<job name, to which you want to copy the steps>') -- Followed by all the msdb.dbo.sp_add_jobstep from the script that scripted out in the earlier step EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'<stepname>', @step_id=1, @cmdexec_success_code=0, @on_success_action=3, @on_success_step_id=0, @on_fail_action=2, @on_fail_step_id=0, @retry_attempts=0, @retry_interval=0, @os_run_priority=0, @subsystem=N'TSQL', @command=N'<code>', @database_name=N'', @flags=0
在Job2中,创建TSQL Command类型的作业步骤。在内容中,让它运行现有作业(sp_start_job)
EXECUTE msdb.dbo.sp_start_job 'Job1'
那将异步运行,因此在它开始调用存储过程之后,它将返回并执行作业的下一步。它不会等待开始的作业完成。如果被调用的作业失败,它将不会渗透回调用作业。
右键单击Job1并脚本到新的查询窗口。对Job2重复该步骤,然后根据需要/在需要的地方将作业步骤从1穿入2。与重新创建轮子相比,单击的次数要少得多,并且希望不会出现更多错误。
从stackoverflow(mafafu)
WAITFOR DELAY '00:00:02';
while exists (select * from msdb.dbo.sysjobs j
inner join msdb.dbo.sysjobactivity a on j.job_id = a.job_id
where name = 'Job 1'
and stop_execution_date is null and start_execution_date is not null)
begin
PRINT 'Waiting...'
WAITFOR DELAY '00:00:02';
end