小编典典

如何在SQL中获取上次运行的作业详细信息

sql

如何在SQL ServerAgent中使用SQL获取上次运行的作业详细信息,包括仅针对上次运行的作业的步骤详细信息(而不是作业结果),因为我想在应用程序中显示此信息

请帮助这个问题坚持很长时间

这是我下面一直在使用的代码,它带回了“作业历史”中所有“作业”的所有步骤,

但是,我只想查看 上次 运行作业的步骤

谢谢

USE msdb
Go 
SELECT j.name JobName,h.step_name StepName, 
CONVERT(CHAR(10), CAST(STR(h.run_date,8, 0) AS dateTIME), 111) RunDate, 
STUFF(STUFF(RIGHT('000000' + CAST ( h.run_time AS VARCHAR(6 ) ) ,6),5,0,':'),3,0,':') RunTime, 
h.run_duration StepDuration,
    case h.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, 
h.message MessageGenerated
FROM sysjobhistory h 
inner join sysjobs j
ON j.job_id = h.job_id

    LEFT JOIN (
                SELECT 
                    [job_id]
                    , [run_date]
                    , [run_time]
                    , [run_status]
                    , [run_duration]
                    , [message]
                    , ROW_NUMBER() OVER (
                                            PARTITION BY [job_id] 
                                            ORDER BY [run_date] DESC, [run_time] DESC
                      ) AS RowNumber
                FROM [msdb].[dbo].[sysjobhistory]
                WHERE [step_id] = 0
            ) AS [sJOBH]
            ON j.[job_id] = [sJOBH].[job_id]
            AND [sJOBH].[RowNumber] = 1

where j.job_id = 'F04E5D3B-C873-448A-805C-C6309A92DAEC'

ORDER BY j.name, h.run_date, h.run_time desc
GO

阅读 176

收藏
2021-03-23

共1个答案

小编典典

希望这会有所帮助,

附加连接到msdb.dbo.sysjobactivity可以显示作业级别的开始/结束时间。使用此范围,您可以指定仅显示最近一次运行的作业步骤。

    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

编辑:在顶部添加了两个参数,@job_id和@job_name。如果未提供@job_id,则首先将尝试确定提供的@job_name的job_id,然后返回上一次运行的作业(不提供任何值)。

2021-03-23