Question

When I'm using Management Studio Express to drop a database there is an checkbox "Close existing connections." which close open database and enforce it to drop.

How can i drop a database programmatic by closing open connections and check it's ever exists before?

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top