首页 > 解决方案 > 如何查询 SQL Server 代理 - 所有作业的错误消息结果集

问题描述

问题:如何查询SQL Server Agent - 所有作业的错误信息结果集

image-20200814085131688

输入:

select *
from [jobs's error masseage table]
where status = 'error' and date between '2020-05-01' and '2020-06-01'

预期结果:

jobname , runtime , status , message
xxx1 , 2020-05-03 , error , #)*)_(#_@$#
xxx2 , 2020-05-05 , error , #)*)_(#_@$#
....

我尝试过并且知道的:我知道Job Activity MonitorGUI 表单可以做到,但它只包含上次运行,如此屏幕截图所示:

image-20200814085609180

标签: sqlsql-server

解决方案


我搜索了 msdn 并找到了 dbo.sysjobhistory (Transact-SQL) - SQL Server | Microsoft Docs可以做到

SELECT sj.name,
        sh.*
FROM msdb.dbo.sysjobs sj
JOIN msdb.dbo.sysjobhistory sh ON sj.job_id = sh.job_id
where run_status = 0 and run_date between '20200806' and '20200811'
order by run_date

image-20200814100930197

但它看起来只包含几天的数据。

image-20200814100844574


推荐阅读