I'm doing active development on my schema in SQL Server 2008 and frequently want to rerun my drop/create database script. When I run
USE [master]
GO
IF EXISTS (SELECT name FROM sys.databases WHERE name = N'MyDatabase')
DROP DATABASE [MyDatabase]
GO
I often get this error
Msg 3702, Level 16, State 4, Line 3
Cannot drop database "MyDatabase" because it is currently in use.
If you right click on the database in the object explorer pane and select the Delete task from the context menu, there is a checkbox which to "close existing connections"
Is there a way to specify this option in my script?