Good Morning Friends,

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 SQL Server 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:

  • 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 failure


Create a table named notification_plants 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 Newyork.


This step involves creation of a stored procedure named usp_send_replication_alerts which contains the required logic.

First, I 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.

When 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 temporary table.

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.

Then a 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.

The initialized 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.

Then send the email to the desired recipients. In order to do this  we need to create a Database Mail Profile. More details on How to create a Database Mail Profile can be found in the Books Online.

That’s it, now you will have a much more user friendly email alerts notification system.

I hope you have enjoyed reading this article. Please do let me know if you have any suggestions regarding a much better approach for the same.



Satnam Singh

Like us on FaceBook Follow us on Twitter

Follow me on FaceBook| Join the fastest growing SQL Server group on FaceBook