sql - 假阴性 : Job Step History

我编写了一个查询以在存储过程中使用,以获取我已设置的每个代理作业中各个步骤的作业步骤历史记录。在我发现我的设计存在缺陷之前,我以为一切正常。

我尝试使用 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_datecase 中:

, 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/

相关文章:

git - 克隆存储库时写入错误

javascript - Javascript 中的网络流量

.net - 如何从 .NET 调用 Oracle 表函数(流水线函数)

google-app-engine - gae 运行时 MCycles

eclipse - 为什么 Maven 不能在 Eclipse Indigo 中工作?

asp.net-mvc-3 - 哪个是公开实体或 DTO 以在 mvc3 中查看的最佳实践?

macos - OSX 信号量 : Invalid argument in sem_open

c# - 从任务计划程序运行时,Windows 服务器上的预定 C# 控制台应用程序不显示控制台

symfony - 如何覆盖供应商捆绑配置?

imagemagick - 使用 rmagick 调整 png 图像的大小而不会降低质量