I have recently been conducting lots of training on SQL Server technology. During these trainings, I quite often create new databases and drop them as well. Many times, I am not able to drop the database as one of my instances might be using the database. As I am working on my laptop and very confident regarding dropping the database, I always take my database in single user and drop it immediately.
ALTER DATABASE [YourDbName]
SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
The above query will rollback any transaction which is running on that database and brings SQL Server database in a single user mode.
The usual error due to database in use is as follows:
Msg 3702, Level 16, State 3, Line 1
Cannot drop database “YourDbName” because it is currently in use.
as the ALTER script mentioned on the top of the post will remove that error. Please make sure that if you are on production server, alter database should be used very carefully.
Reference : Pinal Dave (http://blog.SQLAuthority.com)










Hi Pinal,
I cant get this POST.
I created the database and then dropped the DB..it didnt throw any error…
What is this script doing???
please share once again…
Great post,
Thanks,
Jinesh.
Nitin,
You will not receive the error that Pinal described unless there are open connections to the database in question. Try creating the database, opening a connection, and then attempting a DROP from a different session.
Mihir
What do I need to do if I want to attach the database again?
It is because I need to detach the database and remove the log files and then attach again.
Thank you.
Dave
[...] If you face error that if database is already in use, you can resolve the same by making database in single user mode – here is the guideline SQL SERVER – ALTER DATABASE dbname SET SINGLE_USER WITH ROLLBACK IMMEDIATE. [...]
Other helpful thing that helped was to do a sp_who2 and kill the spids of the users that are connected. Once I did that then I was able to Alter Database
Hello there.
Jasen, there are applications that will reconnect automatically if the connection is dropped (killing the SPIDs), so your solution is good for certain scenarios.
Best regards,
Calin
ALTER DATABASE [Demo]
SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
after run above query my demo database work only single user,if i want to again same database as multiuser then with query apply
Regards
Mangesh
Use
ALTER DATABASE [Demo]
SET MULTI_USER