判断Agent Job完成的存储过程

/******************************************************************************
** File:    sp_WaitForJob.sql
** Name:    [dbo].[WaitForJob]

** Desc:    The proc is used to check agent job run status
**         
**
** Params:
** @jobName            -- description
** @minutesToWait      -- description
** Return
**
** Date:    2011/4/20
** ****************************************************************************
** CHANGE HISTORY
** ****************************************************************************
** Date     Author      version     1.0     #bug              Description
** ----------------------------------------------------------------------------

*/

IF OBJECT_ID ( 'WaitForJob', 'P' ) IS NOT NULL
BEGIN
 PRINT 'Dropping proc dbo.WaitForJob...'
  DROP PROCEDURE WaitForJob;
END  
GO

PRINT 'Creating proc dbo.WaitForJob...'
GO

CREATE PROCEDURE [dbo].[WaitForJob](
    @jobName varchar(100),
    @minutesToWait int
)
AS
BEGIN

    SET NOCOUNT ON;
   
    DECLARE @errorMessage varchar(250)
   
    ------------------------------------------
    -- Get the job id for the specified job.
    ------------------------------------------
    DECLARE @job_id uniqueidentifier SET @job_id = NULL
   
    SELECT @job_id = job_id
    FROM msdb.dbo.sysjobs
    WHERE name = @jobName
   
    IF @job_id IS NULL
   
    BEGIN
  
  SET @errorMessage = 'Do not find agent job with name: ' + @jobName
  RAISERROR(@errorMessage, 16, 1)
  
  RETURN
 END
 
 WAITFOR DELAY '00:00:05'
 
 -------------------------------------------------------------------------------
 -- Check to see if the job is actively running. If so, wait for job finished.  
 -- If job does not complete in specified minutes, raise error.
 -------------------------------------------------------------------------------
 DECLARE @tbl_jobActivity TABLE
 (
  session_id    int,
     job_id     uniqueidentifier,
     job_name    varchar(100),
     run_requested_date  datetime,
     run_requested_source nvarchar(50),
     queued_date    datetime,
     start_execution_date datetime,
     last_executed_step_id int,
     last_executed_step_date datetime,
     stop_execution_date     datetime,
     next_scheduled_run_date datetime,
     job_history_id          int,
     message                 varchar(2000),
     run_status              int,
     operator_id_emailed     int,
     operator_id_netsent     int,
     operator_id_pageid      int
 )
 
 DECLARE @last_executed_step_date  datetime
 DECLARE @stop_execution_date      datetime
 DECLARE @job_history_id           datetime
 
 DECLARE @startWaiting     datetime
 SET @startWaiting = getdate()
 
 WHILE(1=1) -- Loop until job is done
 
  BEGIN
   -- refresh job activity data
   DELETE FROM @tbl_jobActivity
       INSERT @tbl_jobActivity
   EXEC msdb.dbo.sp_help_jobactivity
   
   SELECT @last_executed_step_date = last_executed_step_date,
       @stop_execution_date  = stop_execution_date,
       @job_history_id   = job_history_id
   FROM   @tbl_jobActivity
   WHERE  job_id = @job_id
   
   -- Break from loop if job is no longer running and send the status message, else wait for 20 seconds
   IF
    (@last_executed_step_date IS NOT NULL)
    AND
    (@stop_execution_date IS NOT NULL)
    AND
    (@job_history_id IS NOT NULL)
    
    BREAK;
   ELSE
                WAITFOR DELAY '00:00:20'
 
   -- Raise error if waited for more minutes than specified
   IF DATEDIFF(mm, @startWaiting, getdate()) > @minutesToWait
    BEGIN
     SET @errorMessage = 'ERROR: Waited more than ' + convert(varchar,@minutesToWait) + ' minutes for job to complete.'
     RAISERROR (@errorMessage, 16, 1)
     RETURN
    END
  END
END     
GO

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值