Skip to content

Instantly share code, notes, and snippets.

@tyagiakhilesh
Created January 12, 2018 06:48
Show Gist options
  • Save tyagiakhilesh/063dd3c34786feba157817281b34777c to your computer and use it in GitHub Desktop.
Save tyagiakhilesh/063dd3c34786feba157817281b34777c to your computer and use it in GitHub Desktop.
Dropping multiple databases in mssql
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