Skip to content

Instantly share code, notes, and snippets.

@Toumash
Created October 19, 2021 10:26
Show Gist options
  • Save Toumash/bac1a16e1ed4c7c2f7a945863525cb30 to your computer and use it in GitHub Desktop.
Save Toumash/bac1a16e1ed4c7c2f7a945863525cb30 to your computer and use it in GitHub Desktop.
DECLARE @name VARCHAR(100) -- database name
DECLARE @SQLString nvarchar(200)
DECLARE db_cursor CURSOR FOR
SELECT name
FROM MASTER.dbo.sysdatabases
WHERE name NOT IN ('master','model','msdb','tempdb','spare-me','me-too')
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @name
use master
WHILE @@FETCH_STATUS = 0
BEGIN
SET @SQLString = 'ALTER DATABASE [' + @name +'] SET single_user with ROLLBACK IMMEDIATE;
DROP DATABASE [' + @name +']';
EXECUTE (@SQLString)
FETCH NEXT FROM db_cursor INTO @name
END
CLOSE db_cursor
DEALLOCATE db_cursor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment