I am writing a sql script to build a database. Usually I can just "execute" the entire script which begins with this:
IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'MyDatabase')
DROP DATABASE [MyDatabase]
This tends to always work when I first open query analyzer. However after I mess around with the script enough, eventually it stops working with error:
Server: Msg 3702, Level 16, State 4, Line 6
Cannot drop the database 'MyDatabase' because it is currently in use.
No one else is connected to this database. The only "fix" I know of is to open enterprise manager and go to detach the database (gives me option to punt all users). I punt the users but do not detach database and then the script can be executed.
So - my question is: Is there a SQL query that I can place at the top of this script (before DROP DATABASE) that will punt all the users and save me this hassle?