I am at a new company that has many SQL Servers with hundreds of databases on them. It is suspected that many of these databases are no longer used. I found the below query to run but don't know how to code it to loop through all of the databases with a print command to tell me which databases this applies to. Does anyone have any idea how to show which db the output applies to or know of another SQL method of finding which databases may not have any last read or update stats for?
DECLARE @command varchar(1000)
SELECT @command =
;WITH agg AS
database_id = DB_ID()
last_read = MAX(last_read),
last_write = MAX(last_write)
SELECT last_user_seek, NULL FROM agg
SELECT last_user_scan, NULL FROM agg
SELECT last_user_lookup, NULL FROM agg
SELECT NULL, last_user_update FROM agg
) AS x (last_read, last_write);'
EXEC sp_MSforeachdb @command