2012-02-27 79 views
2

我已经编写了一个查询以用于存储过程以获取每个已安装的作业程序作业中各个步骤的作业步骤历史记录。我认为我有一切工作正常,直到我注意到我的设计中有缺陷。错误否定:作业步骤历史记录

我试图使用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 

回答

0

你可以改变你的case表达被包含在其他case首先检查last_run_date

, 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,但如果作业非常迅速地执行,可以是零.. 。

+0

谢谢!这似乎解决了我遇到的问题。出于某种原因,我从未想过使用2个案例。 – Upstart 2012-02-27 16:46:10