SQL Server: Getting a list of failed agent jobs
Hi All,
Here I try to collect a list of failed jobs from SQL Server agent which are getting failed for a specific time being.
Here I try to collect a list of failed jobs from SQL Server agent which are getting failed for a specific time being.
---------------------------------------------------------------------------------- First create a temp table to Populate data for all failed jobs in last one month (720 hours)
-------------------------------------------------------------------------------
USE msdbGOselect sjh.server as instance, sjst.database_name as database_name, sj.name as job_name,CONVERT(datetime,CONVERT(VARCHAR(8),sjh.run_date),12) as run_date,case sjh.run_statuswhen 1 then 'SUCCEEDED'when 0 then 'FAILED'when 2 then 'RETRY'when 3 then 'CANCELED'end as run_statusinto #temp_job_statusfrom sysjobservers sjs, sysjobhistory sjh, sysjobs sj, sysjobsteps sjstwhere sjs.job_id = sjh.job_idand sjs.job_id = sj.job_idand sjs.job_id = sjst.job_idand sjs.last_run_outcome = 0and sj.enabled = 1and DATEDIFF(hh, CONVERT(datetime,CONVERT(VARCHAR(8),sjh.run_date),12), GETDATE())<= 720;----------------------------------------------------------------- choose the first and last failed date from that table------------------------------------------------------------select database_name, job_name, max(run_date), min(run_date) from #temp_job_statuswhere run_status='FAILED'and database_name <> 'NULL'group by database_name, job_name;------------------------------------- finally drop the table-------------------------------drop table #temp_job_status;
Comments
Post a Comment