SQL Server Agent Job E-Mail Notification Query

I'm about to have a couple of weeks holiday over Christmas (lucky me!) and one of the tasks my boss asked me to do before I went was make sure that e-mail notifications that were directed to me only, should be re-assigned to our entire team.

We rely on SQL Server Agent Jobs and these can be set to notify operators when the job fails, succeeds, or completes (regardless of the outcome). After quickly looking through a couple of jobs, I found plenty that were only notifying me (oops). I needed a more foolproof way to identify the jobs that needed changing.

In addition to setting the e-mail operator to be our team e-mail, I also took the opportunity to also make sure that the job writes to the Event Log on failure and has a valid category (I blogged about Job Categories - and how you can set up your own - back in April 2008).

Here's the query I used, which lists all jobs apart from Reporting Services subscription jobs (which have GUIDs as names) and disabled jobs:

  1. --*** SQL Server Agent Jobs and selected properties, by Thomas Williams ***
  2. --*** this query has been tested on SQL Server 2005 only ***
  3. --For more information on the sysjobs table, see:
  4. --  http://msdn.microsoft.com/en-us/library/ms189817.aspx
  5. --For more information on sysoperators, see:
  6. --  http://msdn.microsoft.com/en-us/library/ms188406.aspx
  7. SELECT  --Job name
  8.         [Job Name] = sj.[name],
  9.         --Job owner
  10.         [Owner] = SUSER_SNAME(sj.[owner_sid]),
  11.         --Job category e.g. "Database Maintenance", "Report Server", "[Uncategorized (Local)]"
  12.         [Category] = c.[name],
  13.         --Operator to be e-mailed (may be NULL)
  14.         [Email Operator] = o.[name],
  15.         --notification in Event log (notify when job fails, succeeds, always, never)
  16.         [Event LOG Notification] = CASE sj.[notify_level_eventlog]
  17.             WHEN 0 THEN 'Never'
  18.             WHEN 1 THEN 'When the job succeeds'
  19.             WHEN 2 THEN 'When the job fails'
  20.             WHEN 3 THEN 'When the job completes (regardless of the job outcome)'
  21.             ELSE 'UNKNOWN' END,
  22.         --e-mail notification (notify when job fails, succeeds, always, never)
  23.         [Email Notification] = CASE sj.[notify_level_email]
  24.             WHEN 0 THEN 'Never'
  25.             WHEN 1 THEN 'When the job succeeds'
  26.             WHEN 2 THEN 'When the job fails'
  27.             WHEN 3 THEN 'When the job completes (regardless of the job outcome)'
  28.             ELSE 'UNKNOWN' END
  29. FROM    msdb.dbo.[sysjobs] sj LEFT OUTER JOIN
  30.             --E-Mail Operator
  31.             msdb.dbo.[sysoperators] o ON
  32.                 sj.[notify_email_operator_id] = o.[id] LEFT OUTER JOIN
  33.             --Job categories
  34.             msdb.dbo.[syscategories] C ON
  35.                 sj.[category_id] = c.[category_id]
  36. WHERE   --ignore auto-created jobs (Reporting Services schedules)
  37.         NOT (sj.[name] LIKE '_____________-____-____-____________') AND
  38.         --only enabled Jobs
  39.         sj.[enabled] = 1
  40. ORDER BY sj.[name]

One thing I came across while auditing jobs was e-mail notifications on success or completion (rather than just on failure). Our team agreed that if we needed a special notification on success or completion that we would use sp_send_dbmail in a job step, and leave the job notifications for errors only.

Tags: , , ,

posted @ Tuesday, December 02, 2008 1:45 PM

Print

Comments on this entry:

# re: SQL Server Agent Job E-Mail Notification Query

Left by Mong at 12/30/2008 1:38 AM
Gravatar
Or you could just set up a redirect on your e-mail.

# re: SQL Server Agent Job E-Mail Notification Query

Left by Thomas Williams at 12/30/2008 10:27 AM
Gravatar
Yep, fair point Mong, though in my situation that would be undesirable as I don't want the team getting all my e-mails.
Comments have been closed on this topic.
«September»
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789