Created
November 18, 2021 13:33
-
-
Save kristianrl/49df63404a510d683d6add00df298a1f to your computer and use it in GitHub Desktop.
List potentially unused MSSQL databases
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'' List potentially unused MSSQL databases | |
'' for a single database | |
EXECUTE sp_msforeachdb 'SELECT TOP 1 * FROM [?].sys.objects ORDER BY modify_date DESC' | |
'' for all databases | |
CREATE TABLE #database_age (db_name VARCHAR(255), name VARCHAR(255), modify_date DATETIME, type_desc VARCHAR(255)) | |
EXECUTE sp_msforeachdb 'INSERT INTO #database_age SELECT TOP 1 ''[?]'' AS db_name, name, modify_date, type_desc FROM [?].sys.objects WHERE type_desc NOT IN (''INTERNAL_TABLE'',''SYSTEM_TABLE'', ''SERVICE_QUEUE'') ORDER BY modify_date DESC' | |
SELECT * FROM #database_age ORDER BY modify_date | |
DROP TABLE #database_age |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment