365DBA Blog

Database Stuff

SQL Server Monitoring Scripts - 7 - SQL Restart Check

This is a simple script to let you know if the SQL instance has been restarted in the past 24 hours (configurable with the @Threshold variable. 

Set NoCount On
Set ANSI_Warnings Off
Select '---------------SQL Restart Check---------------'
Declare @RestartTime DateTime, @Threshold DateTime
Select @RestartTime = crdate From sys.sysdatabases Where [name] = 'tempdb'
Select @Threshold = DateAdd(hh, -24, GetDate())
If DateDiff(mi, @Threshold, @RestartTime) > 5
Select 'Server: ' + Cast(ServerProperty('MachineName') As sysname) + IsNull('\' + Cast(ServerProperty('InstanceName') As sysname), '') + ' restarted at: ' + Cast(@RestartTime As VarChar(25))