T-SQL WHERE用于特定值

时间:2017-01-12 09:40:50

标签: sql-server tsql

如何将此脚本修改为仅选择每个作业名称的最新执行?

z = s

1 个答案:

答案 0 :(得分:0)

根据您的需要更改订单..

SELECT j.name JobName,h.step_name StepName, 
CONVERT(CHAR(10), CAST(STR(h.run_date,8, 0) AS dateTIME), 111) RunDate, 
STUFF(STUFF(RIGHT('000000' + CAST ( h.run_time AS VARCHAR(6 ) ) ,6),5,0,':'),3,0,':') RunTime, 
h.run_duration StepDuration,
case h.run_status when 0 then 'failed'
when 1 then 'Succeded' 
when 2 then 'Retry' 
when 3 then 'Cancelled' 
when 4 then 'In Progress' 
end as ExecutionStatus, 
h.message MessageGenerated
 sysjobs j
 cross apply
 (
 select top 1 b.columns you need 
 from sys.jobhistory b where b.job_id=j.job_id
 order by RunDate desc
 ) h