Saturday, October 3, 2009

Deleting multiple SQL databases using script in SQL 2005

WARNING - Only run this script if you have experience with managing SQL databases. All you have to do is change the wildcard criteria.

/*********************************************************************************
/ START - Delete multiple database SQL script
/*********************************************************************************

USE [master]
GO

DECLARE @command varchar(1000)
/****** Change the prefix WSS_ to whatever you’re prefixing your database names with******/
SELECT @command =
'IF ''?'' NOT IN(''master'', ''model'', ''msdb'', ''tempdb'') AND ''?'' LIKE ''WSS_%''
BEGIN DROP DATABASE [?]
END'
EXEC sys.sp_MSforeachdb @command

/*********************************************************************************
/ END - Delete multiple database SQL script
/*********************************************************************************

Thanks to Laurent for this magical script

No comments:

Post a Comment