SQL Server Email Alerts Notifications
As DBAs, we are often required to set up alert notification
emails for various tasks. For example, there might be a requirement to set up
notification alerts if a particular backup job fails. A while back, I came across the situation
where I was asked to write a script which sent an email alert to a
particular group of users if there was an issue with the Transactional Replication.
The users were not happy with the alert message generated by the pre-defined
alerts introduced by Microsoft in SQL Server Management Studio since they were not able to understand
the description of the message – since the body as well as the heading of the email
were not easily interpreted. After gathering the requirements, I decided to implement
a more functional email alerts system.
- SQL Server 2005(Except Express Edition) or Above
- A valid database mail profile.
I identified that a table named sysreplicationalerts
which is present in the msdb database and contains the entire replication
failure alert message. There are around 14 types of alert messages generated for
- Replication Warning: long merge over dial up connection
- Replication Warning: long merge over LAN connection
- Replication Warning: Slow merge over dial up connection
- Replication Warning: Slow merge over LAN connection
- Replication Warning: Subscription expiration
- Replication Warning: Transactional Replication Latency
- Replication: agent custom shutdown
- Replication: agent failure
- Replication: agent retried
- Replication: agent success
- Replication: expired subscription dropped
- Replication: Subscriber has failed data validation
- Replication: Subscriber has passed data validation
- Replication: Subscription reinitialized after validation
Create a table named notification_subscribers which
contains all the subscriber information i.e subscriber ip address, server
name, and general name if there is any for that server.
Script for the same is as shown below:
Then populate the table with the respective server ip, server
name of the respective subscribers as well as if there is any general name. By
general name, consider for example if a subscriber has been
created for Newyork location, then in the general name we shall include
This step involves creation of a stored procedure named usp_send_replication_alerts
which contains the required logic.
created a table named replicationalerts and subscriber_alert_message.
I then populated the temporary table named replication_alerts with the
data from sysreplication table. This table is present in the msdb
database of the SQL Server.
row_number() over(order by alert_id),[time],subscriber,alert_error_text
datepart(yyyy,[time]) = datepart(yyyy,getdate())
datepart(mm,[time]) = datepart(mm,getdate())
datepart(dd,[time]) = datepart(dd,getdate())
populating the data, bear in mind that only the current date’s data
should be populated. i.e If the script is executed on 23 September 2010 then
only those alerts encountered on 23 September 2010 should be inserted into the
@i = 1
@max_count = max(replication_alert_id) from #replicationalerts
from #replicationalerts where replication_alert_id = @i
= left(@subscribername,@count) from #replicationalerts
= alert_error_message from #replicationalerts where replication_alert_id = @i
= @alert_message_patindexname -1
= len(alert_error_message) from #replicationalerts where replication_alert_id =
= right(@alerterrormessage,@final_count-1) from #replicationalerts
= [time] from #replicationalerts where replication_alert_id = @i
general_name = a.general_name from notification_plants a inner join
a.plant_server_name = b.subscriber_name
@i = @i+1
First the value
of the variable @i is initialized as 1. Then we determined the max value of the
replication_alert_id column present in the temporary table named replicationalerts.
While the value of @i is less than or equal to @max_count the WHILE loop
continues and fetches the required information as shown above.
temporary table named #alert_message is created which fetches the data from the
temporary table named subscriber_alert_message. It fetches only those data
which is between 1 hour.
over(order by subscriber_name,alert_message),
the value of the variable @j as 1 and the WHILE loop continues till the value
of the variable @j is less than or equal to the value of @max_count.
@max_count = max(row_id) from #alert_message
@mail_subject = general_name+space(1)+’Replication Has Failed’ from
#alert_message where row_id = @j
@message = alert_message from #alert_message where row_id = @j
reason for failure is:<br><br>
Pages: 1 2