如何在SQL Agent中创建相互依赖的SQL作业,
例如: 通过脚本创建三个SQL作业,并希望将它们安排为相互依赖,如... 当第一个工作结束时,第二个工作应该开始,第二个工作完成,然后第三个工作应该开始,等等
示例脚本:
DECLARE @MyJobName varchar(50) = 'MyJob01'
EXEC msdb..sp_add_job @job_name =@MyJobName,@enabled=1, @start_step_id=1, @delete_level=1 --Job will delete itself after success
EXEC msdb..sp_add_jobstep @job_name=@MyJobName,@step_id=1, @step_name='STEP1',@command='SELECT * FROM TABLE1'
EXEC msdb..sp_add_jobserver @job_name =@MyJobName, @server_name = N'MySQLServer'
EXEC msdb..sp_start_job @job_name=@MyJobName
SET @MyJobName = 'MyJob02'
EXEC msdb..sp_add_job @job_name =@MyJobName, @enabled=1, @start_step_id=1, @delete_level=1
EXEC msdb..sp_add_jobstep @job_name=@MyJobName,@step_id=1, @step_name='STEP1',@command='SELECT * FROM TABLE2'
EXEC msdb..sp_add_jobserver @job_name =@MyJobName, @server_name = N'MySQLServer'
EXEC msdb..sp_start_job @job_name=@MyJobName
SET @MyJobName = 'MyJob03'
EXEC msdb..sp_add_job @job_name =@MyJobName, @enabled=1, @start_step_id=1, @delete_level=1
EXEC msdb..sp_add_jobstep @job_name=@MyJobName,@step_id=1, @step_name='STEP1',@command='SELECT * FROM TABLE3'
EXEC msdb..sp_add_jobserver @job_name =@MyJobName, @server_name = N'MySQLServer'
EXEC msdb..sp_start_job @job_name=@MyJobName
答案 0 :(得分:0)
作业步骤可以执行SQL Server代理作业。所以你可以创建另一个执行MyJob01的工作,然后是MyJob02,然后是MyJob03。见sp_start_job。像
这样的东西EXEC dbo.sp_start_job N'MyJob01' ;
EXEC dbo.sp_start_job N'MyJob02' ;
EXEC dbo.sp_start_job N'MyJob03' ;