Drop SQL Server Database

g2ieeal7  于 2023-10-15  发布在  SQL Server
关注(0)|答案(2)|浏览(118)

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?

vom3gejh

vom3gejh1#

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.

uqdfh47h

uqdfh47h2#

How long does your conversation takes.. Maybe you should consider using transactions, if that's an option. Just roll the transaction back when your conversation fails.

相关问题