How to get the last run job details in SQL

前端 未结 1 1943
滥情空心
滥情空心 2021-01-06 16:34

How to get the last run job details in SQL Server Agent using SQL including the step details only for the last run job (not the job outcome) as i want to display this in an

相关标签:
1条回答
  • 2021-01-06 16:54

    Hopefully this helps,

    Additional join to msdb.dbo.sysjobactivity shows you the start/end time at a job level. Using this range you can specify to only show you the job steps for the most recent run.

        DECLARE @job_id UNIQUEIDENTIFIER 
                ,@job_name  VARCHAR(256)
    
        SET @job_id = 'DF4C9555-5B24-4649-97CE-5708C53F762C'
        SET @job_name = 'syspolicy_purge_history'
    
        --search for job_id if none was provided
        SELECT  @job_id = COALESCE(@job_id,job_id)
        FROM    msdb.dbo.sysjobs 
        WHERE   name = @job_name
    
        SELECT  t2.instance_id
                ,t1.name as JobName
                ,t2.step_id as StepID
                ,t2.step_name as StepName
                ,CONVERT(CHAR(10), CAST(STR(t2.run_date,8, 0) AS DATETIME), 111) as RunDate
                ,STUFF(STUFF(RIGHT('000000' + CAST ( t2.run_time AS VARCHAR(6 ) ) ,6),5,0,':'),3,0,':') as RunTime
                ,t2.run_duration
                ,CASE t2.run_status WHEN 0 THEN 'Failed'
                                    WHEN 1 THEN 'Succeeded' 
                                    WHEN 2 THEN 'Retry' 
                                    WHEN 3 THEN 'Cancelled' 
                                    WHEN 4 THEN 'In Progress' 
                                    END as ExecutionStatus
                ,t2.message as MessageGenerated    
        FROM    msdb.dbo.sysjobs t1
        JOIN    msdb.dbo.sysjobhistory t2
                ON t1.job_id = t2.job_id   
                --Join to pull most recent job activity per job, not job step
        JOIN    (
                SELECT  TOP 1
                        t1.job_id
                        ,t1.start_execution_date
                        ,t1.stop_execution_date
                FROM    msdb.dbo.sysjobactivity t1
                --If no job_id detected, return last run job
                WHERE   t1.job_id = COALESCE(@job_id,t1.job_id)
                ORDER 
                BY      last_executed_step_date DESC
                ) t3
                --Filter on the most recent job_id
                ON t1.job_id = t3.job_Id
                --Filter out job steps that do not fall between start_execution_date and stop_execution_date
                AND CONVERT(DATETIME, CONVERT(CHAR(8), t2.run_date, 112) + ' ' 
                + STUFF(STUFF(RIGHT('000000' + CONVERT(VARCHAR(8), t2.run_time), 6), 5, 0, ':'), 3, 0, ':'), 121)  
                BETWEEN t3.start_execution_date AND t3.stop_execution_date
    

    Edit: Added two parameters to the top, @job_id and @job_name. If @job_id is not provided, first it'll attempt to determine the job_id of the @job_name provided, and return the last run job is no values are provided.

    0 讨论(0)
提交回复
热议问题