检查SQL Server代理作业状态

时间:2019-05-07 20:51:48

标签: tsql stored-procedures sql-server-2014 sql-agent-job

我受命编写一个查询,每10秒检查一次SQL Server Agent作业的状态,如果作业成功或完成,则返回状态码0以控制m(作业计划程序),如果作业成功则返回1。失败了

有人写过脚本或存储的proc完成这一任务吗?

我试图编写脚本,但是我越看越困惑? 以下是我编写的用于检查作业状态的存储过程。它已编译,但执行时陷入了无休止的循环。我在做什么错?任何帮助将不胜感激。谢谢。

CREATE PROCEDURE [dbo].[uspCheckJobStatusTest2] 
    (
    @JobName NVARCHAR(100),
    @JobStatusCode INT OUTPUT
) 

AS

BEGIN
DECLARE @status INT; 

SET @status = 6; 

 DECLARE @JobStatus_log TABLE (
 LastRunOutcome INT,
 StartExecutionDate datetime NULL,
 StopExecutionDate datetime NULL,
  RunStatus   INT
 );

WHILE ( @status <> 1 OR @status <>9 OR @status <> 0) 
   BEGIN
         BEGIN 
           waitfor delay '00:01'; 

           INSERT INTO @JobStatus_log

            SELECT 
          --@status =  
                     sjs.last_run_outcome  
                     ,sja.start_execution_date 
                     ,sja.stop_execution_date 
                     ,sjh.run_status 

                       FROM msdb.dbo.sysjobs sj
                 JOIN msdb.dbo.sysjobhistory sjh 
                  on sj.job_id = sjh.job_id 
                JOIN msdb.dbo.sysjobactivity sja
                ON sj.job_id = sja.job_id
                JOIN msdb.dbo.sysjobservers sjs 
                 ON ( sj.job_id = sjs.job_id ) 


           WHERE sj.Name=@JobName 
                AND sja.start_execution_date is not null
                --AND sja.stop_execution_date is null
                AND sjs.last_run_date = CONVERT(varchar(10), Getdate(), 112) 
                AND sjh.instance_id=(SELECT MAX(sjh2.instance_id) FROM msdb.dbo.sysjobhistory sjh2 WHERE job_id = (select distinct job_id from msdb.dbo.sysjobs sj2 where sj2.Name=@JobName))
                AND sja.session_id = (
                    SELECT MAX(session_id) FROM msdb.dbo.sysjobactivity WHERE job_id=(select distinct job_id from msdb.dbo.sysjobs sj2 where sj2.Name=@JobName));

           END   

         BEGIN
             IF EXISTS ( SELECT * FROM @JobStatus_log 
             WHERE LastRunOutcome =1
             AND StartExecutionDate IS NOT NULL
             AND StopExecutionDate IS NOT NULL
             AND RunStatus=1
             )
             SET @status=0 --succeeded 
             SELECT @JobStatusCode=@status;

         END

         BEGIN
             IF EXISTS ( SELECT * FROM @JobStatus_log 
             WHERE LastRunOutcome =0
             AND StartExecutionDate IS NOT NULL
             AND StopExecutionDate IS NOT NULL
             AND RunStatus=0)
             SET @status=1 --failed 
             SELECT @JobStatusCode=@status;

         END

        BEGIN
             IF EXISTS ( SELECT * FROM @JobStatus_log 
             WHERE LastRunOutcome IS NOT NULL
             AND StartExecutionDate IS NOT NULL
             AND StopExecutionDate IS NULL
             AND (RunStatus=2 OR RunStatus=4))--In Progress
             CONTINUE

         END


          BEGIN
             IF EXISTS ( SELECT * FROM @JobStatus_log 
             WHERE LastRunOutcome IS NOT NULL
             AND StartExecutionDate IS NOT NULL
             AND StopExecutionDate IS NULL
             AND (RunStatus=2 OR RunStatus=4)
             )--In Progress
             CONTINUE

         END


          BEGIN
             IF EXISTS ( SELECT * FROM @JobStatus_log 
             WHERE LastRunOutcome IS NOT NULL
             AND StartExecutionDate IS NOT NULL
             AND StopExecutionDate IS NOT NULL
             AND (RunStatus=3)
                   )--Canceled
             SET @status=9;
             SELECT @JobStatusCode=@status;

          END
END

END 开始

0 个答案:

没有答案