Tuesday, June 03, 2008

Forcefully Disconnect All Users and Drop a Database

Yesterday, one of my colleague was telling me that he was not able to delete the database. When I checked the issue at his desk, I saw that other users were connected to that database without his knowledge and hence he was getting error while deleting it.

One way to get rid of it immediately:

ALTER DATABASE MyDatabase
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
DROP DATABASE MyDatabase

~ Kiran

2 comments:

Mohit Batra said...

There are times when you just have to do some operation and disconnect the users. So, its not a good option to switch it to single user mode, though that is fine if its on a standalone machine for testing purposes. So, better thing is to disconnect the users only (or kill their processes), rather than switching DB to single user mode.

Kiran Marke said...

Mohit, since the main purpose was to delete the database...it doesnt matter whether you switch it into single_ user or not before dropping it...

But yes, as you say, there are times when you just have to disconnect other users from the database...We can acheive it by just killing all the processes connected to that particular database.
Also see my post...
Disconnect all users from database