Tuesday, December 21, 2010

Delete multiple databases in SQL Server

I've been into positions multiple times where I've to delete the databases in my SQL Server environment to clean up the QA environment or my local machine where I accumulate these test DBs over a period of time. If you are looking for one such situation here is the script that can come as a help -


declare

@command varchar (1000)

SELECT @command = 'IF ''?'' NOT IN(''master'', ''model'', ''msdb'', ''tempdb'') BEGIN USE master DROP DATABASE ? END'

exec SP_MSforeachdb @command

No comments: