Email Alert when SQL Server is restarted

As a DBA we need to be notified when a SQL Server is restarted so that we can take a proactive reaction to identify the cause of the restart which could be an indication of underlining issues with the Server OS or the SQL Server Engine.

I have used this several years now and it has helped me avoid Severity 1 outages a few cases.

Just put this code in a sql job and make sure you change the db mail portion that pertains to your environment.

Declare @Title varchar(max)
Declare @Ebody varchar(max)
Declare @TableHead varchar(max)
Declare @TableTail varchar(max)

Select @Title = 'SQL Server Restarted - ' + @@SERVERNAME
Set NoCount On;
Set @TableTail= '</body></html>';

---HTML Layout--

Set @TableHead = '<html><head>' +
'<H4 style="color: #000000">**********This is an informational message only**********</H4>' +
 '</head>' +
 '<body>' + 'SQL Server Services have been RESTARTED : '+ '<strong>' + @@SERVERNAME + '</strong>' +
 '<p>' + ' If this was not planned, check the Server' + '</p>'

select @TableHead = @TableHead + @TableTail

WAITFOR DELAY '00:00:30'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'DB_Admins',
@recipients = 'db_admins@yourdomain.com',
@body = @TableHead,
@body_format = HTML,
@subject = @Title;

Hope this helps you avoid any outages as it has helped me.

Leave a Reply

Your email address will not be published.