Script out all alerts

Carolyn Richardson, 2016-04-06 (first published: 2014-05-13)

I found that no alerts had been added to any servers in a company I started work at, as there were many servers, I decided to set up one server with all the alerts I thought they should have and script the results to apply elsewhere. This script excludes any replication alerts so the resulting script can be used to add alerts on other servers.

You could always go a step further and run this in an SSIS package to apply across a whole environment.

Here's the result when I ran it:-

IF (EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = N'Error Number 824'))

 —- Delete the alert with the same name.

  EXECUTE msdb.dbo.sp_delete_alert @name = N'Error Number 824'

BEGIN

EXECUTE msdb.dbo.sp_add_alert @name = N'Error Number 824', @message_id = 824 , @severity = 0 , @enabled = 1, @delay_between_responses = 60 , @include_event_description_in = 1, @category_name = N'[Uncategorized]'

END

IF (EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = N'Error Number 825: Miscellaneous System Information'))

 —- Delete the alert with the same name.

  EXECUTE msdb.dbo.sp_delete_alert @name = N'Error Number 825: Miscellaneous System Information'

BEGIN

EXECUTE msdb.dbo.sp_add_alert @name = N'Error Number 825: Miscellaneous System Information', @message_id = 825 , @severity = 0 , @enabled = 1, @delay_between_responses = 60 , @include_event_description_in = 1, @category_name = N'[Uncategorized]'

END

IF (EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = N'Severity 016: Miscellaneous User Error'))

 —- Delete the alert with the same name.

  EXECUTE msdb.dbo.sp_delete_alert @name = N'Severity 016: Miscellaneous User Error'

BEGIN

EXECUTE msdb.dbo.sp_add_alert @name = N'Severity 016: Miscellaneous User Error', @message_id = 0 , @severity = 16 , @enabled = 1, @delay_between_responses = 60 , @include_event_description_in = 1, @category_name = N'[Uncategorized]'

END

IF (EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = N'Severity 017: Insufficient Resources'))

 —- Delete the alert with the same name.

  EXECUTE msdb.dbo.sp_delete_alert @name = N'Severity 017: Insufficient Resources'

BEGIN

EXECUTE msdb.dbo.sp_add_alert @name = N'Severity 017: Insufficient Resources', @message_id = 0 , @severity = 17 , @enabled = 1, @delay_between_responses = 60 , @include_event_description_in = 1, @category_name = N'[Uncategorized]'

END

IF (EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = N'Severity 018: NonFatal Internal Error'))

 —- Delete the alert with the same name.

  EXECUTE msdb.dbo.sp_delete_alert @name = N'Severity 018: NonFatal Internal Error'

BEGIN

EXECUTE msdb.dbo.sp_add_alert @name = N'Severity 018: NonFatal Internal Error', @message_id = 0 , @severity = 18 , @enabled = 1, @delay_between_responses = 60 , @include_event_description_in = 1, @category_name = N'[Uncategorized]'

END

IF (EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = N'Severity 019: Fatal Error in Resource'))

 —- Delete the alert with the same name.

  EXECUTE msdb.dbo.sp_delete_alert @name = N'Severity 019: Fatal Error in Resource'

BEGIN

EXECUTE msdb.dbo.sp_add_alert @name = N'Severity 019: Fatal Error in Resource', @message_id = 0 , @severity = 19 , @enabled = 1, @delay_between_responses = 60 , @include_event_description_in = 1, @category_name = N'[Uncategorized]'

END

IF (EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = N'Severity 020: Fatal Error in current Proccess'))

 —- Delete the alert with the same name.

  EXECUTE msdb.dbo.sp_delete_alert @name = N'Severity 020: Fatal Error in current Proccess'

BEGIN

EXECUTE msdb.dbo.sp_add_alert @name = N'Severity 020: Fatal Error in current Proccess', @message_id = 0 , @severity = 20 , @enabled = 1, @delay_between_responses = 60 , @include_event_description_in = 1, @category_name = N'[Uncategorized]'

END

IF (EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = N'Severity 021: Fatal Error in Database Processes'))

 —- Delete the alert with the same name.

  EXECUTE msdb.dbo.sp_delete_alert @name = N'Severity 021: Fatal Error in Database Processes'

BEGIN

EXECUTE msdb.dbo.sp_add_alert @name = N'Severity 021: Fatal Error in Database Processes', @message_id = 0 , @severity = 21 , @enabled = 1, @delay_between_responses = 60 , @include_event_description_in = 1, @category_name = N'[Uncategorized]'

END

IF (EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = N'Severity 022: Fatal Error: Table Integrity Suspect'))

 —- Delete the alert with the same name.

  EXECUTE msdb.dbo.sp_delete_alert @name = N'Severity 022: Fatal Error: Table Integrity Suspect'

BEGIN

EXECUTE msdb.dbo.sp_add_alert @name = N'Severity 022: Fatal Error: Table Integrity Suspect', @message_id = 0 , @severity = 22 , @enabled = 1, @delay_between_responses = 60 , @include_event_description_in = 1, @category_name = N'[Uncategorized]'

END

IF (EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = N'Severity 023: Fatal Error Database Integrity Suspect'))

 —- Delete the alert with the same name.

  EXECUTE msdb.dbo.sp_delete_alert @name = N'Severity 023: Fatal Error Database Integrity Suspect'

BEGIN

EXECUTE msdb.dbo.sp_add_alert @name = N'Severity 023: Fatal Error Database Integrity Suspect', @message_id = 0 , @severity = 23 , @enabled = 1, @delay_between_responses = 60 , @include_event_description_in = 1, @category_name = N'[Uncategorized]'

END

IF (EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = N'Severity 024: Fatal Error Hardware Error'))

 —- Delete the alert with the same name.

  EXECUTE msdb.dbo.sp_delete_alert @name = N'Severity 024: Fatal Error Hardware Error'

BEGIN

EXECUTE msdb.dbo.sp_add_alert @name = N'Severity 024: Fatal Error Hardware Error', @message_id = 0 , @severity = 24 , @enabled = 1, @delay_between_responses = 60 , @include_event_description_in = 1, @category_name = N'[Uncategorized]'

END

IF (EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = N'Severity 025: Fatal Error'))

 —- Delete the alert with the same name.

  EXECUTE msdb.dbo.sp_delete_alert @name = N'Severity 025: Fatal Error'

BEGIN

EXECUTE msdb.dbo.sp_add_alert @name = N'Severity 025: Fatal Error', @message_id = 0 , @severity = 25 , @enabled = 1, @delay_between_responses = 60 , @include_event_description_in = 1, @category_name = N'[Uncategorized]'

END

    /*
--Purpose:  To script all SQL Alerts, so the the resulting script can be applied to add alerts on to another server
--Author:	Carolyn Richardson
--Date:		13/05/2014
*/


--Run on source server
USE MSDB
GO

SELECT 'IF (EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = N'''+NAME+'''))
 ---- Delete the alert with the same name.
  EXECUTE msdb.dbo.sp_delete_alert @name = N'''+name+''' 
BEGIN 
EXECUTE msdb.dbo.sp_add_alert @name = N'''+name+''', @message_id = '+CAST(message_id AS VARCHAR(10))+' , @severity = '+CAST(severity AS VARCHAR(10))+' , @enabled = 1, @delay_between_responses = '+CAST(delay_between_responses AS VARCHAR(10))+' , @include_event_description_in = '+CAST(include_event_description AS VARCHAR(10))+', @category_name = N''[Uncategorized]''
END
' FROM [msdb].[dbo].[sysalerts]
WHERE category_id <> 20


--Add email notifications amend DBA to your operator
SELECT 'EXEC msdb.dbo.sp_add_notification @alert_name=N'''+NAME+''', @operator_name=N''DBA'', @notification_method = 7;'
FROM [msdb].[dbo].[sysalerts]
WHERE category_id <> 20

--Run results on destination server

Rate

3.67 (3)

Share

Share

Rate

3.67 (3)

Related content

The Importance of Maintenance on MSDB

MSDB is a system database used by SQL Server. MSDB stores all sorts of data, such as backup history, log shipping monitor history, SSIS packages and Service Broker queue data to name a few. Just like user databases, MSDB needs regular maintenance, including index optimizations and, more importantly, regular purging. In this article, Tim Radney looks at how neglecting your MSDB can negatively impact on your environment.

Additional Articles

2015-07-28

6,714 reads

Controlling Growth of an msdb Database

I recently encountered a situation where the drive hosting Sharepoint Databases in a Staging environment ran out of space. I logged onto the server and found that the msdb database has itself occupied 38 GB of the total disk space. Msdb database generally contain maintenance information for the database such as backups, log shipping and so on.

Additional Articles

2012-01-25

2,711 reads