|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Monday, January 12, 2009 9:39 AM
Points: 7,
Visits: 11
|
|
Thanks for posting this script. I'm having trouble modifying it to my use though. I'm trying to create a script that I can have dbmail run to email me the last run status of every job. The script given, if I remove the where section works, except it gives the last run for each type of status. If there's been a failure and a success within it's history it shows both with dates.
Any ideas on how I can just have it show the late run status and time?
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 1:58 AM
Points: 6,351,
Visits: 5,361
|
|
SELECT j.[name], CAST(STUFF(STUFF(CAST(jh.run_date as varchar),7,0,'-'),5,0,'-') + ' ' + STUFF(STUFF(REPLACE(STR(jh.run_time,6,0),' ','0'),5,0,':'),3,0,':') as datetime) AS [LastRun], CASE jh.run_status WHEN 0 THEN 'Failed' WHEN 1 THEN 'Success' WHEN 2 THEN 'Retry' WHEN 3 THEN 'Canceled' WHEN 4 THEN 'In progress' END AS [Status] FROM (SELECT a.job_id,MAX(a.instance_id) As [instance_id] FROM msdb.dbo.sysjobhistory a WHERE a.step_id = 0 GROUP BY a.job_id) b INNER JOIN msdb.dbo.sysjobhistory jh ON jh.instance_id=b.instance_id INNER JOIN msdb.dbo.sysjobs j ON j.job_id = jh.job_id
Far away is close at hand in the images of elsewhere. Anon.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Monday, January 12, 2009 9:39 AM
Points: 7,
Visits: 11
|
|
| Thank you. That's exactly what I was looking for.
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 4:02 AM
Points: 34,
Visits: 606
|
|
| Thank you for the script Wildcat, it's very useful for me!
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Tuesday, April 23, 2013 4:37 PM
Points: 5,
Visits: 112
|
|
SELECT j.[name], MAX(CAST( STUFF(STUFF(CAST(jh.run_date as varchar),7,0,'-'),5,0,'-') + ' ' + STUFF(STUFF(REPLACE(STR(jh.run_time,6,0),' ','0'),5,0,':'),3,0,':') as datetime)) AS [LastRun], MAX(CAST( STUFF(STUFF(CAST(sjs.next_run_date as varchar),7,0,'-'),5,0,'-') + ' ' + STUFF(STUFF(REPLACE(STR(sjs.next_run_time,6,0),' ','0'),5,0,':'),3,0,':') as datetime)) AS [NextRun], CASE jh.run_status WHEN 0 THEN 'Failed' WHEN 1 THEN 'Success' WHEN 2 THEN 'Retry' WHEN 3 THEN 'Canceled' WHEN 4 THEN 'In progress' END AS Status FROM msdb.dbo.sysjobs j INNER JOIN msdb.dbo.sysjobhistory jh ON jh.job_id = j.job_id AND jh.step_id = 0 inner join msdb.dbo.syscategories sc on j.category_id = sc.category_id INNER JOIN MSDB.dbo.sysjobschedules sjs on j.job_id = sjs.Job_id GROUP BY j.[name], jh.run_status
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Wednesday, May 01, 2013 5:15 AM
Points: 236,
Visits: 235
|
|
Word of warning : if the history has been purged/deleted then sysjobhistory will not display the same data as "Job Activity Monitor" i.e. "Job Activity Monitor" will display a last run date but this is not obtainable from running queries against sysjobhistory where the history has been purged because it just won't exist.
Use sysjobactivity to obtain the "real" last run date and to be in agreement with "Job Activity Monitor".
Try it; create a test job, run it a few times, manually delete the latest history records then run your query using sysjobhistory. You will find that your queries report an earlier last run time than SQL's "Job Activity Monitor".
Does it matter ? Well it may do, I noticed this problem when deciding which jobs could be deleted. It seemed that some jobs were never run but this was not the case as the history had been deleted.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Friday, February 01, 2013 6:32 PM
Points: 20,
Visits: 157
|
|
Do you have the script please to use sysjobactivity? I would like to bring all the jobs with their names, steps, and the status, last run time, even if there is no history, or the jobs is disabled, not scheduled
|
|
|
|