SQL Server: Single User Mode Basics

Taking DB into SINGLE USER mode:

For some maintenance situations, it is useful to obtain exclusive access to a SQL Server database. This can be achieved by setting the database's user access into single user mode, which permits only one connection to be made to the database at any time.

Limitations and Restrictions

  • If other users are connected to the database at the time that you set the database to single-user mode, their connections to the database will be closed without warning.
  • The database remains in single-user mode even if the user that set the option logs off. At that point, a different user, but only one, can connect to the database.

Prerequisites

  • Before you set the database to SINGLE_USER, verify that the AUTO_UPDATE_STATISTICS_ASYNC option is set to OFF. When this option is set to ON, the background thread that is used to update statistics takes a connection against the database, and you will be unable to access the database in single-user mode. For more information, see ALTER DATABASE SET Options (Transact-SQL).

Check the AUTO_UPDATE_STATISTICS condition

exec sp_helpdb dbname


Status=ONLINE, Updateability=READ_WRITE, UserAccess=MULTI_USER, Recovery=SIMPLE, Version=661, Collation=Latin1_General_CI_AS, SQLSortOrder=0, IsAutoCreateStatistics, IsAutoUpdateStatistics, IsFullTextEnabled

 
ALTER DATABASE dbname SET AUTO_UPDATE_STATISTICS OFF


Status=ONLINE, Updateability=READ_WRITE, UserAccess=MULTI_USER, Recovery=SIMPLE, Version=661, Collation=Latin1_General_CI_AS, SQLSortOrder=0, IsAutoCreateStatistics, IsFullTextEnabled



Finally to set a database to single-user mode
USE master;
GO

ALTER DATABASE dbname
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
GO

ALTER DATABASE dbname
SET READ_ONLY;
GO

ALTER DATABASE dbname
SET MULTI_USER;
GO

This example sets the database to SINGLE_USER mode to obtain exclusive access. The example then sets the state of the 'dbname' database to READ_ONLY and returns access to the database to all users.The termination option WITH ROLLBACK IMMEDIATE is specified in the first ALTER DATABASE statement. This will cause all incomplete transactions to be rolled back and any other connections to the AdventureWorks2012 database to be immediately disconnected.

Source:

http://www.mssqltips.com/sqlservertip/1193/autoupdatestatisticsasync-sql-server-database-configuration/


Comments

Popular posts from this blog

Restoring Master, Model and MSDB from Netbackup

Oracle Job Operations