@@ -3742,6 +3742,14 @@ AS
37423742
37433743 IF @Debug IN (1 , 2 ) RAISERROR (' Running CheckId [%d].' , 0 , 1 , 94 ) WITH NOWAIT ;
37443744
3745+ ;WITH las_job_run AS (
3746+ SELECT MAX (instance_id) AS instance_id,
3747+ job_id, COUNT_BIG (* ) AS job_executions,
3748+ SUM (CASE WHEN run_status = 0 THEN 1 ELSE 0 END ) AS failed_executions
3749+ FROM msdb .dbo .sysjobhistory
3750+ WHERE step_id = 0
3751+ GROUP BY job_id
3752+ )
37453753 INSERT INTO #BlitzResults
37463754 ( CheckID ,
37473755 Priority ,
@@ -3756,8 +3764,32 @@ AS
37563764 ' Agent Jobs Without Failure Emails' AS Finding ,
37573765 ' https://www.brentozar.com/go/alerts' AS URL ,
37583766 ' The job ' + [name]
3759- + ' has not been set up to notify an operator if it fails.' AS Details
3767+ + ' has not been set up to notify an operator if it fails.'
3768+ + CASE
3769+ WHEN jh .run_date IS NULL OR jh .run_time IS NULL OR jh .run_status IS NULL THEN ' '
3770+ ELSE N ' Executions: '+ CAST (ljr .job_executions AS VARCHAR (30 ))
3771+ + CASE ljr .failed_executions
3772+ WHEN 0 THEN N ' '
3773+ ELSE N ' ('+ CAST (ljr .failed_executions AS NVARCHAR (10 )) + N ' failed)'
3774+ END
3775+ + N ' - last execution started on '
3776+ + CAST (CONVERT (DATE ,CAST (jh .run_date AS NVARCHAR (8 )),113 ) AS NVARCHAR (10 ))
3777+ + N ' , at '
3778+ + STUFF (STUFF (RIGHT (N ' 000000' + CAST (jh .run_time AS varchar (6 )),6 ),3 ,0 ,N ' :' ),6 ,0 ,N ' :' )
3779+ + N ' , with status "'
3780+ + CASE jh .run_status
3781+ WHEN 0 THEN N ' Failed'
3782+ WHEN 1 THEN N ' Succeeded'
3783+ WHEN 2 THEN N ' Retry'
3784+ WHEN 3 THEN N ' Canceled'
3785+ WHEN 4 THEN N ' In Progress'
3786+ END + N ' ".'
3787+ END AS Details
37603788 FROM msdb.[dbo].[sysjobs] j
3789+ LEFT JOIN las_job_run ljr
3790+ ON ljr .job_id = j .job_id
3791+ LEFT JOIN msdb.[dbo].[sysjobhistory] jh
3792+ ON ljr .job_id = jh .job_id AND ljr .instance_id = jh .instance_id
37613793 WHERE j .enabled = 1
37623794 AND j .notify_email_operator_id = 0
37633795 AND j .notify_netsend_operator_id = 0
0 commit comments