forked from himadanreddy/SQL-DBA-SCRIPTS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdd Email Notification For Failed SQL Server Agent Jobs.sql
More file actions
33 lines (29 loc) · 1.22 KB
/
Add Email Notification For Failed SQL Server Agent Jobs.sql
File metadata and controls
33 lines (29 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
--Create notifications for failed jobs with one easy script
--Here an easy way to setup alerts for all sql agent jobs.
--Typically I use this to send an email alert when a job fails.
-- I always setup a group email address rather than individuals to make this a one-time setup.
USE [msdb]
GO
--create a DBA Team operator
EXEC msdb.dbo.sp_add_operator @name=N'Production DBA Team', -- Change this to the operator you need to be notified.
@enabled=1,
@email_address=N'DBAS@yourdomain.com' -- Change this to the email address of the operator.
GO
--add notifications for failure to all jobs
DECLARE @QuotedIdentifier char(1); SET @QuotedIdentifier = '' -- use '''' for single quote
DECLARE @ListDelimeter char(1); SET @ListDelimeter = ';'
DECLARE @CSVlist varchar(max) --use varchar(8000) for SQL Server 2000
--no event log, email on failure
SELECT @CSVlist = COALESCE(@CSVlist + @ListDelimeter, '') + @QuotedIdentifier +
'
EXEC msdb.dbo.sp_update_job @job_id=N'''
+ convert(varchar(max),[job_id]) +
''',
@notify_level_eventlog=0,
@notify_level_email=2,
@notify_email_operator_name=N''DBA Team''' -- Change this to the operator you need to be notified.
+ @QuotedIdentifier
from msdb.dbo.sysjobs
print @csvlist
--EXEC (@CSVlist)
GO