I' using a SQL Server 2008 Sp3 environment.
I have a query that returns information about failed jobs from the last 10 minutes.
If a job has two schedulers (for example – a job that runs every 10 minutes in the morning, and every 30 minutes in the rest of the day), the query returns two rows.
Can you help me fix the query so it would return only the relevant row of the relevant scheduler that was used ?
The query I'm using is pasted below:
SELECT h.server
,j.name
,h.step_id
,h.step_name
,h.message
,h.run_status
,Rundatetime = CONVERT(DATETIME, CONVERT(CHAR(8), run_date, 112) + ' ' + STUFF(STUFF(RIGHT('000000' + CONVERT(VARCHAR(8), run_time), 6), 5, 0, ':'), 3, 0, ':'), 121)
,h.run_duration
,s.last_outcome_message
,sc.name AS ScheduleName
,s.last_run_time
,sjc.next_run_time
FROM msdb..sysjobhistory h
INNER JOIN msdb..sysjobs j ON h.job_id = j.job_id
INNER JOIN msdb..sysjobservers s ON s.job_id=h.job_id
LEFT OUTER JOIN msdb..sysjobschedules sjc ON sjc.job_id=h.job_id
LEFT OUTER JOIN msdb..sysschedules sc ON sc.schedule_id=sjc.schedule_id
WHERE run_status=0
AND step_name<>'(Job outcome)'
--Looking on failed job from the last 10 minutes.
AND CONVERT(DATETIME, CONVERT(CHAR(8), run_date, 112) + ' ' + STUFF(STUFF(RIGHT('000000' + CONVERT(VARCHAR(8), run_time), 6), 5, 0, ':'), 3, 0, ':'), 121) > DATEADD(mi, -10, GETDATE())
ORDER BY h.run_date DESC ,h.run_time DESC
Thanks in advance,
Roni.
Best Answer
This is a little scrappy, but it is a start...
I have extracted the Schedule ID from the [last_outcome_message] field, and joined it to the Schedule ID. That should give you the correct result set
I could not find anywhere else where the schedule_id is stored against a History Event.