Created
January 12, 2018 06:48
-
-
Save tyagiakhilesh/063dd3c34786feba157817281b34777c to your computer and use it in GitHub Desktop.
Dropping multiple databases in mssql
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
use master | |
go | |
declare @dbnames nvarchar(max) | |
declare @statement nvarchar(max) | |
set @dbnames = '' | |
set @statement = '' | |
select @dbnames = @dbnames + ',[' + name + ']' from sys.databases where name like 'datbase_name_%' | |
if len(@dbnames) = 0 | |
begin | |
print 'no databases to drop' | |
end | |
else | |
begin | |
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