查询MSSQL作业的运行时长 –作业时长监控

 SELECT top 1000  j.name                        AS [作业名],  
        h.step_id                     AS 步骤Id,  
        h.step_name                   AS 步骤名称 ,  
        h.message                     AS [作业消息]        ,  
        h.run_date                    AS [运行日期]       ,  
        h.run_time                    AS [运行时间]       ,  
        msdb.dbo.agent_datetime(h.run_date, h.run_time)   
                                    AS '作业开始运行时间' ,  
        CAST(run_duration / 10000 AS VARCHAR(2)) + N'小时'  
        + CAST(( run_duration - run_duration / 10000 * 10000 ) / 100 AS VARCHAR(2)) + N'分钟'  
        + SUBSTRING(CAST(run_duration AS VARCHAR(10)),  
                            LEN(CAST(run_duration AS VARCHAR(10))) - 1, 2)  + N'秒'  
        AS [运行耗时]  
FROM    msdb.dbo.sysjobhistory h  
        LEFT JOIN msdb.dbo.sysjobs j ON h.job_id = j.job_id  
WHERE  
    --- j.name ='作业名称' and  作业名称
    ---Run_Date = CAST(CONVERT(VARCHAR(8), GETDATE(), 112) AS INT)  --今天执行的作业  
    ---AND
     h.run_status = 1    --执行成功  
ORDER BY h.run_date desc