|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 2:16 AM
Points: 68,
Visits: 425
|
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, August 24, 2012 2:43 AM
Points: 1,
Visits: 84
|
|
Hi Lars. Very nice Scripts , if you could supply the startdate, ( SQL Server was started: Dec 15 2009 9:56AM Time since SQL Server service was started: 33 days 23 hours 17 minutes.) in the Output - It will fullfill it. Thanks
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 2:16 AM
Points: 68,
Visits: 425
|
|
Hi jks-591682
To get the start time of the service, you could add this line:
PRINT 'SQL Server was started: ' + CONVERT(char,@starttime)
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: 2 days ago @ 6:19 AM
Points: 46,
Visits: 284
|
|
Lars,
you can also retrieve the start time from this Dynamic Management View
SELECT sqlserver_start_time FROM sys.dm_os_sys_info
(Requires VIEW SERVER STATE permission on the server)
Marco
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 2:16 AM
Points: 68,
Visits: 425
|
|
mchofman (2/4/2010) Lars,
you can also retrieve the start time from this Dynamic Management View
SELECT sqlserver_start_time FROM sys.dm_os_sys_info
(Requires VIEW SERVER STATE permission on the server)
Marco
Hi Marco
That's correct for SQL Server 2008. The sqlserver_start_time in sys.dm_os_sys_info was added in SQL Server 2008 and wasn't there in SQL Server 2005.
Using the tempdb works for all our current versions :) (yes, we still have SQL Server 2000)
Kind regards
Lars
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: 2 days ago @ 6:19 AM
Points: 46,
Visits: 284
|
|
Lars,
I didn't know it isn't available in SQL 2005. Nice to know for a project I'm working. Thanks for your solution!
Regards Marco
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Friday, April 05, 2013 3:42 AM
Points: 291,
Visits: 1,064
|
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 1:49 AM
Points: 1,131,
Visits: 855
|
|
Hej Lars,
For SQL Server 2008 you need to change line 1 from SELECT crdate FROM sysdatabases WHERE name = 'tempdb' to SELECT create_date FROM sys.databases WHERE name = 'tempdb'
Med venlig hilsen Henrik Staun Poulsen Stovi Software
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 2:16 AM
Points: 68,
Visits: 425
|
|
Hi Henrik,
actually you don't need it, but I agree it would be better practice for SQL Server 2005 as well as SQL Server 2008 to use the sys.databases view which is available from SQL Server 2005 and forward.
Do do this while still maintaining the compatibility with SQL Server 2000, you could replace this:
-- determines when tempdb was created (done at startup) DECLARE @starttime datetime SET @starttime = (SELECT crdate FROM sysdatabases WHERE name = 'tempdb' )
With this:
-- determines when tempdb was created (done at startup) DECLARE @starttime datetime DECLARE @sql_version sysname SET @sql_version = convert(sysname, (SELECT SERVERPROPERTY('Productversion')))
IF (@sql_version NOT LIKE '8.%') BEGIN -- SQL Server 2005 and forward SET @starttime = (SELECT create_date FROM sys.databases WHERE name = 'tempdb') END ELSE BEGIN -- SQL Server 2000 SET @starttime = (SELECT crdate FROM sysdatabases WHERE name = 'tempdb') END
The premises for this however is that you don't execute it on versions older than SQL Server 2000, but I hope you don't have any of them running anyway 
Best regards
Lars Søe Mikkelsen JN Data
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Friday, December 31, 2010 9:46 AM
Points: 274,
Visits: 473
|
|
Very nice! You can shorten it up a bit though 
DECLARE @minutesSinceSQLStarted int
SELECT @minutesSinceSQLStarted = DATEDIFF(MINUTE, -- determines when tempdb was created (done at startup) (SELECT crdate FROM master.dbo.sysdatabases WHERE name = 'tempdb'), GETDATE())
PRINT 'Time since SQL Server service was started: ' + CONVERT(varchar(4), @minutesSinceSQLStarted / (60*24)) + ' days ' + CONVERT(varchar(2), @minutesSinceSQLStarted % (60*24) / 60) + ' hours ' + CONVERT(varchar(2), @minutesSinceSQLStarted % (60*24) % 60) + ' minutes.'
Scott Pletcher, SQL Server MVP 2008-2010
|
|
|
|