Locked yourself out of a database? get access back …


For any purpose, you set a database [named: test] to a single-user mode and forget to set AUTO_UPDATE_STATISTICS OFF before doing so, use a script to remove the single-user mode (it’s most likely the GUI dialog isn’t accessible, so the quick option is to use T-SQL) and gain access to the database back.

The background thread that is used to update statistics takes a connection against the database leaving the user unable to access the database, because, it’s now in a single-user mode.

The single-user mode flag will remain even if the user account that sets it disconnected.

USE test;
GO

-- optional block
ALTER DATABASE test
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
GO

-- optional block
ALTER DATABASE test
SET READ_WRITE;
GO

-- This will set it back on to multi-user mode.
ALTER DATABASE test
SET MULTI_USER;
GO

-- Finally 
ALTER DATABASE test
SET AUTO_UPDATE_STATISTICS ON

The option to set restrict access to single-user mode is found in the database properties dialog box.