Wednesday, April 20, 2011

Find How Long The SQL Server Services Has Been Running

Here is the script which I have created to find out how long the SQL server services has been running /up.

Find out how long the SQL Server service has been running?
USE Master
GO
SET NOCOUNT ON
DECLARE 
@crdate DATETIME, 
@hr VARCHAR(50), 
@min VARCHAR(5)

SELECT @crdate=crdate  

FROM sysdatabases WHERE NAME='tempdb'
SELECT @hr=(DATEDIFF ( mi, @crdate,GETDATE()))/60
IF ((DATEDIFF ( mi, @crdate,GETDATE()))/60)=0
SELECT @min=(DATEDIFF ( mi, @crdate,GETDATE()))
ELSE
SELECT 
@min=(DATEDIFF ( mi, @crdate,GETDATE()))-((DATEDIFF( mi, @crdate,GETDATE()))/60)*60
PRINT 'SQL Server "' + CONVERT(VARCHAR(20),SERVERPROPERTY('SERVERNAME'))+'" is Online for the past '+@hr+' hours & '+@min+' minutes'
IF NOT EXISTS (SELECT 1 FROM master.dbo.sysprocesses WHERE program_name = N'SQLAgent - Generic Refresher')
BEGIN
PRINT 'SQL Server is running but SQL Server Agent <> running'
END
ELSE BEGIN
PRINT 'SQL Server and SQL Server Agent both are running'
END 

Find out how long the sql server service has been running?
SELECT 'Sql Server Service has been running for about '
+ CAST((DATEDIFF(hh, create_date, GETDATE()))/24 AS varchar(3)) + ' days and '
+ CAST((DATEDIFF(hh, create_date, GETDATE())) % 24 AS varchar(2)) + ' hours'
FROM sys.databases
WHERE name = 'tempdb'

Find out how long the sql server service has been running?
SELECT create_date
FROM sys.databases
WHERE name = 'tempdb'
Find out how long the sql server service has been running, Database Engine Service Startup Options, How long has my Sql Server been running, How long has my Sql Server been running, How long has SQL Server instance been Running, How long has a database been running, When did a job start and how long has it been running, How long the SQLServer Agent Job is running, SQL Server has ben running.

2 comments:

Please Give me Some Example or Practicle About File And FileGroups...
in Which Conditions File And FileGroup are Useful for Our database?

Post a Comment