Monday, June 8, 2009

"cannot drop database because it is currently in use"

A common mistake is usually made when executing the "drop database db_name" statement is forgetting to terminate the current session connected to the target database. A quick solution is to switch to master db and then drop it.

use master
drop database db_name

Most of the time, this can succeed. If the same error message still pops up, disconnect query analyzer or management studio from the SQL Server and reconnect to it and run the above t-sql command again.

2 comments:

  1. use master
    go

    alter database [db_name] set single_user with rollback immediate
    go

    ReplyDelete
  2. The best way to to flush out all the users currently logged on to the database is to set the database into single user mode and the script in the last comment does exactly this.

    ReplyDelete