mssql get jobstate by name

DECLARE @job_state INT     
Exec msdb..sp_get_jobstate_byname 'job_name',@job_state out   
select @job_state  
  
----------------  
use msdb  
go  
  
create procedure dbo.sp_get_jobstate_byname  
    @job_name             varchar(100)   
    ,@job_state         int out    
AS    
BEGIN    
    DECLARE @is_sysadmin INT    
    DECLARE @job_owner   sysname     
    DECLARE @job_id  UNIQUEIDENTIFIER  
    
    SET NOCOUNT ON    
    
    CREATE TABLE #xp_results (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)    
    
    select  @Job_ID=Job_ID from msdb.dbo.sysjobs where name = @job_name  
      
    -- Need a job_id    
    if (@Job_ID IS NULL)    
    BEGIN     
        RAISERROR(14262, -1, -1, '@job_name', @job_name)    
        RETURN(1) -- Failure    
    END    
    
    -- Capture job execution information (for local jobs only since that's all SQLServerAgent caches)    
    SELECT @is_sysadmin = ISNULL(IS_SRVROLEMEMBER(N'sysadmin'), 0)    
    SELECT @job_owner = suser_sname(suser_sid())    
    INSERT INTO #xp_results    
    EXECUTE master.dbo.xp_sqlagent_enum_jobs @is_sysadmin, @job_owner    
    
    -- Select the job state of the job in question    
    SELECT @job_state = job_state FROM #xp_results WHERE @job_id = job_id    
        
    -- All done    
    DROP TABLE #xp_results    
    RETURN(0)    -- Success    
END    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值