简体   繁体   中英

How to find the SQL Server databse Last used?

How to find the SQL Server database Last used by DB users. ... give you whodunnit information and you can group by to get min/MAX event times

WITH myCTE AS
(
    SELECT
        DB_NAME(database_id) AS TheDatabase,
        last_user_seek,
        last_user_scan,
        last_user_lookup,
        last_user_update
    FROM 
        sys.dm_db_index_usage_stats
)
SELECT
    ServerRestartedDate = (SELECT CREATE_DATE FROM sys.databases where name='tempdb'),
    x.TheDatabase,
    MAX(x.last_read) AS  last_read,
    MAX(x.last_write) AS last_write
FROM
    (SELECT 
         TheDatabase, last_user_seek AS last_read, NULL AS last_write 
     FROM 
         myCTE

     UNION ALL  

     SELECT 
         TheDatabase, last_user_scan, NULL 
     FROM 
         myCTE

     UNION ALL

     SELECT 
         TheDatabase, last_user_lookup, NULL 
     FROM 
         myCTE

     UNION ALL

     SELECT 
         TheDatabase, NULL, last_user_update 
     FROM 
         myCTE) AS x
GROUP BY 
    TheDatabase

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM