这是一个脚本,它将检查作业的状态并在它尚未运行时运行它。
declare @xp_results table (
job_id UNIQUEIDENTIFIER NOT NULL,
last_run_date INT NOT NULL,
last_run_time INT NOT NULL,
next_run_date INT NOT NULL,
next_run_time INT NOT NULL,
next_run_schedule_id INT NOT NULL,
requested_to_run INT NOT NULL, -- BOOL
request_source INT NOT NULL,
request_source_id sysname COLLATE database_default NULL,
running INT NOT NULL, -- BOOL
current_step INT NOT NULL,
current_retry_attempt INT NOT NULL,
job_state INT NOT NULL)
DECLARE @job_id uniqueidentifier ;
select @job_id = job_id from msdb.dbo.sysjobs where name = 'Job1';
insert into @xp_results
EXEC master.dbo.xp_sqlagent_enum_jobs 1, sa, @job_id
select case when running = 1 then 'Currently Running' else '' end as running,
case job_state
when 0 then 'Not Idle or Suspended'
when 1 then 'Executing Job'
when 2 then 'Waiting For Thread'
when 3 then 'Between Retries'
when 4 then 'Idle'
when 5 then 'Suspended'
when 6 then 'WaitingForStepToFinish'
when 7 then 'PerformingCompletionActions'
end as job_state
from @xp_results
IF (select running from @xp_results) <> 1
EXEC msdb.dbo.sp_start_job 'Job1'