Enforce SQL Server to drop a database
When I'm using Management Studio Express
to drop a database there is an checkbox "Close existing connections."
which close open database a开发者_JAVA百科nd enforce it to drop.
How can i drop a database programmatic by closing open connections and check it's ever exists before?
The output of the "script" icon on the drop screen, as suggested in the comments by Randolph Potter (this assumes that your database name is "test"):
EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = N'test'
GO
USE [master]
GO
ALTER DATABASE [test] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
USE [master]
GO
/****** Object: Database [test] Script Date: 06/30/2010 09:12:29 ******/
DROP DATABASE [test]
GO
精彩评论