[code language="sql"]
with JobStatus as
(
select
job_id AS JobId,
row_number() over(partition by job_id order by run_date desc, run_time desc) asExecutionRank,
case run_status when 0 then 'Failed' when 1 then 'Succeeded' when 2 then 'Retry' when3 then 'Canceled' end as LastRunStatus
from
msdb.dbo.sysjobhistory
)
select
j.job_id as JobId,
j.name as JobName,
j.[description] as JobDescription,
j.[enabled] as IsJobEnabled,
js.LastRunStatus,
ja.run_requested_date as LastRunDate,
ja.next_scheduled_run_date as NextRunDate,
datediff(second, ja.start_execution_date, ja.stop_execution_date) as JobDuration
from
msdb.dbo.sysjobs j
join msdb.dbo.sysjobactivity ja on j.job_id = ja.job_id
left join JobStatus js on j.job_id = js.JobId
and js.ExecutionRank = 1
order by
JobName,
NextRunDate
[/code]
No comments:
Post a Comment