Created
August 9, 2020 02:17
-
-
Save MartinMiles/4c3fb28c929fe2bd48d9f5397283714f to your computer and use it in GitHub Desktop.
A PowerShell snippet to call SQL command to remove all DBs starting with 'Platform'
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
Invoke-Sqlcmd -ServerInstance "(local)" -Query "` | |
DECLARE @dbnames NVARCHAR(MAX) SET @dbnames = ''` | |
DECLARE @alter NVARCHAR(MAX) SET @alter = ''` | |
DECLARE @statement NVARCHAR(MAX) SET @statement = ''` | |
` | |
SELECT @dbnames = @dbnames + ',[' + name + ']' FROM sys.databases WHERE NAME LIKE 'Platform_%'` | |
SELECT @alter = @alter + 'ALTER DATABASE [' + name + '] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;' FROM sys.databases WHERE NAME LIKE 'Platform_%'` | |
IF LEN(@dbnames) > 0` | |
BEGIN` | |
EXEC sp_executesql @alter` | |
SET @statement = 'drop database ' + SUBSTRING(@dbnames, 2, LEN(@dbnames))` | |
PRINT @statement` | |
EXEC sp_executesql @statement` | |
END" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment