-
Notifications
You must be signed in to change notification settings - Fork 84
/
Last user access for each database.sql
60 lines (58 loc) · 1.97 KB
/
Last user access for each database.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
-- Last user access to each database
-- Part of the SQL Server DBA Toolbox at https://github.com/DavidSchanzer/Sql-Server-DBA-Toolbox
-- This script contains 2 SELECT statements.
-- The first shows the last access date & time for each user database on the instance, using then last_user_% columns in
-- dm_db_index_usage_stats.
-- The second shows the last access date & time for a nominated table, using the same method.
SELECT d.name AS DBname,
LastUserAccess =
(
SELECT LastUserAccess = MAX(a.xx)
FROM
(
SELECT xx = MAX(last_user_seek)
WHERE MAX(last_user_seek) IS NOT NULL
UNION
SELECT xx = MAX(last_user_scan)
WHERE MAX(last_user_scan) IS NOT NULL
UNION
SELECT xx = MAX(last_user_lookup)
WHERE MAX(last_user_lookup) IS NOT NULL
UNION
SELECT xx = MAX(last_user_update)
WHERE MAX(last_user_update) IS NOT NULL
) a
)
FROM master.dbo.sysdatabases d
LEFT OUTER JOIN sys.dm_db_index_usage_stats s
ON d.dbid = s.database_id
WHERE d.dbid > 4
GROUP BY d.name
ORDER BY LastUserAccess;
-- Last user access to a particular table
USE <DBName>;
SELECT d.name AS DBname,
o.name AS [Table],
LastUserAccess =
(
SELECT LastUserAccess = MAX(a.xx)
FROM
(
SELECT xx = MAX([last_user_lookup])
UNION
SELECT xx = MAX([last_user_scan])
UNION
SELECT xx = MAX([last_user_seek])
UNION
SELECT xx = MAX([last_user_update])
) a
)
FROM master.dbo.sysdatabases d
INNER JOIN sys.dm_db_index_usage_stats s
INNER JOIN sys.objects o
ON o.object_id = s.object_id
ON d.dbid = s.database_id
WHERE s.database_id = DB_ID()
AND s.object_id = OBJECT_ID('<TableName>')
GROUP BY d.name,
o.name;