How to drop multiple databases in SQL Server
this is easy...
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 'name.of.db%'
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
Why not just do this instead?
USE master;
Go
SELECT 'DROP DATABASE ['+ name + ']'
FROM sys.databases WHERE name like '_database_name_%';
GO
Capture the output of that resultset and then paste it into another query window. Then run that. Why write all this TSQL cursor code?
"When you have a hammer, everything looks like a nail!"..