Monday, July 1, 2013

Database stuck in single user mode

If you are getting error messages like this:

Msg 5064, Level 16, State 1, Line 1
Changes to the state or options of database 'DbName' cannot be made at this time. The database is in single-user mode, and a user is currently connected to it.
Msg 5069, Level 16, State 1, Line 1
ALTER DATABASE statement failed.

use master
kill 52-- the connection to the database in single user mode
use [DbName]
alter database [DbName] set multi_user with rollback immediate

Or:

use master
kill 52-- the connection to the database in single user mode
alter database [DbName] set offline with rollback immediate
alter database [DbName] set online, multi_user with rollback immediate