I made this Query because we fix databases localy on a workstation (we have no sql server) but when backup up the server they need to be deleted localy that could sometimes take a while.
U can run this Query from the QA or ad it as a Job it's possible to run it from an executable.
A little more about Keepdb and Previewdb.
If you want/need to keep more databases just add the line:
AND name NOT IN ('Keepdb1','Keepdb2','Keepdb3')
If you want/need to keep just one add the line:
AND name NOT IN ('Keepdb1')
Type the line right below this line:
SELECT name FROM sysdatabases WHERE sid 0x01
Previewdb: It means that you can see which databases will be dropped make sure that it looks like this
-- before: exec sp_executesql @SQL
print @SQL
If you happy with the result you can just remove the -- and print @SQL
Happy dropping
2007-10-02 (first published: 2002-06-20)
15,446 reads