HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

When did a specific database come online?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
comedidonlinedatabasespecificwhen

Problem

How can I determine when the state of a specific database switched to online?

Background: I want to determine how long the database is in use to find out how reliable sys.dm_db_index_usage_stats currently is.

Solution

As @SQLLearner specificed in his answer, the easiest way without auditing in place or an XE session running would be to just look in the SQL Server Log. The below code will get the most recent startup of a particular database (using YourDatabaseName in this example, but obviously change it for your environment):

create table #error_log
(
    LogDate datetime not null,
    ProcessInfo nvarchar(128) not null,
    Text nvarchar(512) not null
)
go

insert into #error_log
exec xp_readerrorlog 0, 1, N'Starting', N'YourDatabaseName'
go

select top 1
    LogDate,
    Text
from #error_log
order by LogDate desc


As per @usr's comment an Connect link, in SQL Server 2012 Unicode parameters are required.

Code Snippets

create table #error_log
(
    LogDate datetime not null,
    ProcessInfo nvarchar(128) not null,
    Text nvarchar(512) not null
)
go

insert into #error_log
exec xp_readerrorlog 0, 1, N'Starting', N'YourDatabaseName'
go

select top 1
    LogDate,
    Text
from #error_log
order by LogDate desc

Context

StackExchange Database Administrators Q#20518, answer score: 2

Revisions (0)

No revisions yet.