sp_add_alert (Transact-SQL)
Creates an alert.
sp_add_alert [ @name = ] 'name'
[ , [ @message_id = ] message_id ]
[ , [ @severity = ] severity ]
[ , [ @enabled = ] enabled ]
[ , [ @delay_between_responses = ] delay_between_responses ]
[ , [ @notification_message = ] 'notification_message' ]
[ , [ @include_event_description_in = ] include_event_description_in ]
[ , [ @database_name = ] 'database' ]
[ , [ @event_description_keyword = ] 'event_description_keyword_pattern' ]
[ , { [ @job_id = ] job_id | [ @job_name = ] 'job_name' } ]
[ , [ @raise_snmp_trap = ] raise_snmp_trap ]
[ , [ @performance_condition = ] 'performance_condition' ]
[ , [ @category_name = ] 'category' ]
[ , [ @wmi_namespace = ] 'wmi_namespace' ]
[ , [ @wmi_query = ] 'wmi_query' ]
sp_add_alert must be run from the msdb database.
These are the circumstances under which errors/messages generated by SQL Server and SQL Server applications are sent to the Windows application log and can therefore raise alerts:
-
Severity 19 or higher sys.messages errors
-
Any RAISERROR statement invoked with WITH LOG syntax
-
Any sys.messages error modified or created using sp_altermessage
-
Any event logged using xp_logevent
SQL Server Management Studio provides an easy, graphical way to manage the entire alerting system and is the recommended way to configure an alert infrastructure.
If an alert is not functioning properly, check whether:
-
The SQL Server Agent service is running.
-
The event appeared in the Windows application log.
-
The alert is enabled.
-
Events generated with xp_logevent occur in the master database. Therefore, xp_logevent does not trigger an alert unless the @database_name for the alert is 'master' or NULL.
The following example adds an alert (Test Alert) that runs the Back up the AdventureWorks2012 Database job when fired.
Note
|
|---|
|
This example assumes that the message 55001 and the Back up the AdventureWorks2012 Database job already exist. The example is shown for illustrative purposes only. |
USE msdb ;
GO
EXEC dbo.sp_add_alert
@name = N'Test Alert',
@message_id = 55001,
@severity = 0,
@notification_message = N'Error 55001 has occurred. The database will be backed up...',
@job_name = N'Back up the AdventureWorks2012 Database' ;
GO
