SQL Server: Last Database Access Date and Time

sqlserver2014

Sometimes you need to know when was the last time a database was accessed. You can query this data from SQL Server DMV, as long as the SQL Server services haven’t been restarted.

SELECT name as DatabaseName, 
       last_access =(select X1= max(LA.xx)
from ( select xx =max(last_user_seek) 
       where max(last_user_seek)is not null
       union all
       select xx = max(last_user_scan) 
       where max(last_user_scan)is not null
       union all
       select xx = max(last_user_lookup)
       where max(last_user_lookup) is not null
       union all
       select xx =max(last_user_update)
       where max(last_user_update) is not null) LA)
FROM master.dbo.sysdatabases sd left outer join 
     sys.dm_db_index_usage_stats s on sd.dbid= s.database_id 
group by sd.name;
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s