SELECT
t.name AS TableName,
t.create_date,
COALESCE(i.last_user_seek, i.last_user_scan, i.last_user_lookup, i.last_user_update) AS LastAccess
FROM
sys.tables t
LEFT JOIN
sys.dm_db_index_usage_stats i ON t.object_id = i.object_id
WHERE
i.last_user_seek IS NULL
AND i.last_user_scan IS NULL
AND i.last_user_lookup IS NULL
AND i.last_user_update IS NULL
OR DATEDIFF(DAY, COALESCE(i.last_user_seek, i.last_user_scan, i.last_user_lookup, i.last_user_update), GETDATE()) > 30 -- 60 days
AND t.is_ms_shipped = 0 -- Exclude system tables
ORDER BY
t.create_date;
Share This with your friend by choosing any social account