Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop SQL Server Database

I am creating a SQL Server database programmatically during a conversion. If the conversion code fails I want to delete/drop the database. If I use the shortcut menu for the database in SQL Server Management Studio 2005 the "delete" option is disabled. DROP DATABASE command also fails with message "Cannot drop database "XYZ" because it is currently in use."

I have shutdown and restarted the SQL Server and the database will not drop.

Any direction?

like image 383
Rick Schummer Avatar asked Jun 30 '09 18:06

Rick Schummer


1 Answers

A new search found the following script that worked:

ALTER DATABASE [dbname]
SET SINGLE_USER --or RESTRICTED_USER
WITH ROLLBACK IMMEDIATE;
GO
DROP DATABASE [dbname];
GO

Must have been some open transaction that was stopping the drop. Problem solved.

like image 105
Rick Schummer Avatar answered Oct 06 '22 16:10

Rick Schummer