Find out how long the sql server service has been running, from t-sql

后端 未结 6 1399
春和景丽
春和景丽 2021-02-07 22:11

I\'d like if its possible to work out from inside sql server how long sql server has been running.

Would like to use this in conjunction with one of the DMV\'s for unuse

6条回答
  •  眼角桃花
    2021-02-07 22:44

    Grabbed here

    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
    

提交回复
热议问题