Wednesday, August 7, 2013

How to get list of all the failed SQL Agent Jobs in SQL Server


/***************************************************************************     
* Title : How to find the list of all the failed SQL Jobs in SQL Server      
* Problem Description: There are instances where we may need to quickly see   
  the list of failed SQL Jobs in SQL Server      
****************************************************************************/ 


SELECT H.SERVER AS [SERVER],

      J.[NAME] AS [NAME],

      H.MESSAGE AS [MESSAGE],

      CONVERT(NVARCHAR(100),H.RUN_DATE) + CONVERT(NVARCHAR(100),H.RUN_TIME) AS LastRun

FROM SYSJOBHISTORY H

      INNER JOIN SYSJOBS J

            ON H.JOB_ID = J.JOB_ID

      INNER JOIN (SELECT MAX(H.INSTANCE_ID) AS INSTANCE_ID

                  FROM SYSJOBHISTORY H

                  GROUP BY (H.JOB_ID)) T 

            ON H.INSTANCE_ID = T.INSTANCE_ID

WHERE J.[ENABLED] = 1
AND H.RUN_STATUS = 0   
                 

No comments:

Post a Comment