Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to drop multiple databases in SQL Server

Just to clarify, ths isn't really a question, more some help for people like me who were looking for an answer.
A lot of applications create temp tables and the like, but I was surprised when Team Foundation Server created 80+ databases on my test SQL Server. TFS didn't install correctly, and kindly left me to clear up after it. Since each database had a naming convention, rather than delete each database by hand, I remembered how to use cursors and have written what I view to be the most unwise piece of T-SQL ever:

   CREATE TABLE #databaseNames (name varchar(100) NOT NULL, db_size varchar(50), owner varchar(50), dbid int, created date, status text, compatibility_level int); INSERT #databaseNames     exec sp_helpdb;  DECLARE dropCur CURSOR FOR     SELECT name FROM #databaseNames WHERE name like '_database_name_%'; OPEN dropCur; DECLARE @dbName nvarchar(100); FETCH NEXT FROM dropCur INTO @dbName; DECLARE @statement nvarchar(200); WHILE @@FETCH_STATUS = 0 BEGIN     SET @statement = 'DROP DATABASE ' + @dbName;     EXEC sp_executesql @statement;     FETCH NEXT FROM dropCur INTO @dbName; END CLOSE dropCur; DEALLOCATE dropCur; DROP TABLE #databaseNames; 

It goes without saying that using cursors like this is probably really dangerous, and should be used with extreme caution. This worked for me, and I haven't seen any further damage to my database yet, but I disclaim: use this code at your own risk, and back up your vital data first!
Also, if this should be deleted because it's not a question, I understand. Just wanted to post this somewhere people would look.

like image 387
Gargravarr Avatar asked Feb 25 '11 10:02

Gargravarr


People also ask

How do I drop multiple databases?

Paste all of them in an excel/some other text file (I prefer NPP). Keep the only names which you want to delete from the list. Dont forget to remove your working db's from the list. Add DROP DATABASE in front of those names.

How do I drop all databases in SQL Server?

You can do this through the SSMS GUI. Select the Databases node then F7 to bring up Object Explorer Details, Select all databases that you want to delete, Hit "Delete" and select the "Close Existing Connections" and "Continue after error" options.

Can we drop multiple databases in MySQL?

Be careful before you delete multiple databases in MySQL. It cannot be reversed. By default, DROP DATABASE command allows you to delete only 1 database at a time. So if you want to delete multiple databases in MySQL, you need to run separate DROP DATABASE command for each database.

How do I drop a database in SQL Server by closing existing connections?

Drop Database in SQL Server Using SQL Server Management Studio. Connect to SQL Server Management Studio; expand Database Node -> Right click the Databases which you want to Drop -> Select Delete from the drop-down menu to open up Delete Object dialog box as shown in the snippet below.


1 Answers

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!"..

like image 191
OFH Avatar answered Oct 09 '22 22:10

OFH