我编写了一个查询以在存储过程中使用,以获取我已设置的每个代理作业中各个步骤的作业步骤历史记录。在我发现我的设计存在缺陷之前,我以为一切正常。
我尝试使用 msdb.dbo.sysjobsteps 中的“job_outcome”列来确定作业是失败、成功还是被取消,直到我注意到一些从未执行过的步骤返回为“失败”。我现在意识到我还必须查看“last_run_duration”列,但我不确定如何重新处理我的查询以包含它。我应该尝试不同的方法,还是有人可以建议我如何修改下面的案例陈述来解决问题?
select convert(varchar(75), j.name) as [JobName]
, s.step_id
, convert(varchar(75), s.step_name) as [StepName]
, case s.last_run_outcome
when 1 then 'Success'
when 0 then 'Failed'
when 3 then 'Cancelled' end as [StepStatus]
, h.message
, max(s.last_run_date) as last_run_date
, max(s.last_run_time) as last_run_time
, MAX(s.last_run_duration) as last_run_duration
, max(ja.next_scheduled_run_date) as next_run
from msdb.dbo.sysjobs j
inner join msdb.dbo.sysjobsteps s on j.job_id = s.job_id
left join msdb.dbo.sysjobhistory h on s.job_id = h.job_id
and s.step_id = h.step_id
and s.last_run_date = h.run_date
and s.last_run_time = h.run_time
left join msdb.dbo.sysjobactivity ja on s.job_id = ja.job_id
where j.enabled = 1
group by j.name, s.step_id, s.step_name, s.last_run_outcome, h.message
order by j.name, s.step_id
最佳答案
您可以更改您的 case
表达式以包含在另一个首先检查 last_run_date
的 case
中:
, case when MAX(s.last_run_date) > 0 then
case s.last_run_outcome
when 1 then 'Success'
when 0 then 'Failed'
when 3 then 'Cancelled' end
else 'Never Ran' end as [StepStatus]
您建议检查 last_run_duration
,但如果作业执行得非常快,它可以为零...
https://stackoverflow.com/questions/9467482/