How to Alter a SQL Server Database as Single User Mode and as Multi User Mode

by Shawn November 20, 2009 05:51

Original Link: http://www.kodyaz.com/articles/alter-single-user-multi-user-mode.aspx

You can use the SQL Server Enterprise Manager for SQL Server 2000 to set a database to a single user mode or to multi user mode. Similarly, SQL Server Management Studio can be used for SQL Server 2005 for changing a database to single user mode or to multi user mode. Also you can alter the database access mode by using sql commands like ALTER DATABASE and sp_dboption.

 

ALTER DATABASE [Works] SET MULTI_USER WITH NO_WAIT

ALTER DATABASE [Works] SET SINGLE_USER WITH NO_WAIT

or

EXEC sp_dboption 'Works', 'single user', 'false'

EXEC sp_dboption 'Works', 'single user', 'true'

To alter the database state to restrict the access to the single user mode, first open the Microsoft SQL Server Management Studio, and connect to a SQL Server instance. Open the list of available databases belonging to the related instance in the Object Explorer window. Right click on the sql server database that you want to set its mode to single user and select Properties in the context menu.

When you click properties menu item, the following Database Properties screen will be displayed for the selected database. I used database named Works for samples used for the article.

restrict access

Select the Options page from the list in the left side of the screen. If you scroll down the options list for State options, you will see Restrict Access database options with three options listed in the combo box.

Restrict Access modes : Multiple, Single and Restricted modes.

 

If you select Single mode and click the OK button, you can either alter the database access mode to single user successfully or you will fail to change the access mode because of existence of active open connections to the Works database. The Management Studio IDE will prompt you to close all other connections to the related database for a successfull alter database option process.

open connections

To change the database properties, SQL Server must close all other connections to the database. Are you sure you want to change the properties and close all other connections?
 

After the alter command runs successfully, the database Works will be displayed as shown in the Object Explorer window.
single-user

 

If an active connection exists other than the Management Studio, and you click the database Works within the SQL Server Management Studio, the following warning message will be displayed:


database-is-not-accessible

The database Works is not accessible. (ObjectExplorer)

 

If you right click on the database Works, the following error message will be displayed.

database-is-already-open

Database 'Works' is already open and can only have one user at a time. (Microsoft SQL Server, Error: 924)

 

After a database is altered as single user mode, it can be altered again back to multi user mode by running the below sql command.

ALTER DATABASE [Works] SET MULTI_USER WITH NO_WAIT

But if this command is run from a connection that is not the only active connection to the related database, the command will fail with the following message:

Msg 5064, Level 16, State 1, Line 1
Changes to the state or options of database 'Works' 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.

 

 

In a case that you want to alter the database access mode from single user mode to multi user mode or from multi user mode to single user mode, you may have to kill all the active open connections to the database.

In such a case, you have to find all the open connections and run "Kill @spid" command to close all the connections.

For a list of open connections for a specific database you can run a similar command as below :

select spid from master..sysprocesses where dbid = db_id('Works') and spid <> @@spid

The second part of the task will be closing the open connections with the Kill command. This can be achived by running kill commands within a cursor or creating a dynamic sql command which kills the active connections to the related database.

Let's code the closing open connections using a t-sql cursor:

DECLARE @DatabaseName nvarchar(50)
DECLARE @SPId int

SET @DatabaseName = N'Works'

DECLARE my_cursor CURSOR FAST_FORWARD FOR
SELECT SPId FROM MASTER..SysProcesses WHERE DBId = DB_ID(@DatabaseName) AND SPId <> @@SPId

OPEN my_cursor

FETCH NEXT FROM my_cursor INTO @SPId

WHILE @@FETCH_STATUS = 0
BEGIN
KILL @SPId

FETCH NEXT FROM my_cursor INTO @SPId
END

CLOSE my_cursor
DEALLOCATE my_cursor

 

After all connections are closed except the currently running prcess (@@spid) if we are trying to alter the access mode of the database that we're in from multi user mode to single user mode, we can now run the ALTER DATABASE command for single user mode or multi user mode.

ALTER DATABASE [Works] SET MULTI_USER WITH NO_WAIT

or

ALTER DATABASE [Works] SET SINGLE_USER WITH NO_WAIT

 

You can also use the following sp_dboption sql commands to alter the database option for single user. If you set the "single user" database option to "true", this is same as altering the database as "SINGLE_USER" with ALTER DATABASE statement.


USE master
GO
EXEC sp_dboption 'Works', 'single user', 'false';


If you run the below sp_dboption command which sets the "single user" to "false", this is same as altering the database as "MULTI_USER" with ALTER DATABASE statement.


USE master
GO
EXEC sp_dboption 'Works', 'single user', 'true';

 

The main difference between sp_dboption and ALTER DATABASE commands is that, sp_dboption will not be supported with further releases of SQL SERVER after SQL SERVER 2005.

Tags:

Development

Comments

8/22/2010 7:21:42 PM #

Fat Loss

I have not yet read this post, but it seems intersting.I would love to read more in the future. Keep up the amazing work.

Fat Loss United States

8/26/2010 9:27:28 AM #

Wayne Kotecki

Great post. I just discovered your site and wanted to say that I have certainly liked reading your blog posts. Anyway I'm going to be subscribing to your feed and I hope you write more soon.

Wayne Kotecki United States

8/27/2010 1:39:24 AM #

Electric Cigarettes

I’d love to see a follow up on this.

Electric Cigarettes United States

8/27/2010 2:48:44 PM #

publish articles for free submission

Posting articles to article directories is a fantastic way to gain exposure. While

publish articles for free submission United States

8/27/2010 7:37:07 PM #

Royce Atamanczyk

nice blog... why don't you post more?

Royce Atamanczyk United States

8/28/2010 3:38:02 PM #

Lawerence Kintner

do you have an rss feed? I want to add it to my reader but I can't find it...

Lawerence Kintner United States

8/28/2010 7:25:26 PM #

Melony Thoeny

are you using wp for this blog?

Melony Thoeny United States

8/29/2010 8:46:08 AM #

seduction affiliate

hey, with so many comments you should add an affiliate program to your blog and make some money...

seduction affiliate United States

8/29/2010 8:36:07 PM #

Clarita Pronovost

I bookmarked your blog but I hope you will post more...

Clarita Pronovost United States

8/31/2010 3:02:11 AM #

Jamal Thommarson

While this subject may be incredibly touchy for plenty of people, my opinion is that there has to be a middle or standard ground that we all can discover. I do appreciate that you've additional relevant and intelligent commentary here though. Thank you!

Jamal Thommarson United States

8/31/2010 11:40:25 AM #

blonde jokes

nice blog! keep the good work!

blonde jokes United States

8/31/2010 9:24:37 PM #

pick up blog

good work! keep posting more. I have a dating blog too but it's not so popular like yours..

pick up blog United States

8/31/2010 10:52:21 PM #

gold overmantle mirror

Substantially, the post is actually the greatest on that worthw hile topic. I harmonise with your conclusions and will thirstily look forward to your coming updates. Just saying thanks will not simply just be enough, for the extraordinary lucidity in your writing. I will instantly grab your rss feed to stay abreast of any kind of updates. Fabulous work and also much success in your business enterprize!

gold overmantle mirror United States

9/1/2010 5:44:35 PM #

personal training books

don't forgot to get out and exercise...

personal training books United States

9/2/2010 7:22:57 PM #

gift ideas

try to change your template, there are some great ones...

gift ideas United States

9/2/2010 10:22:35 PM #

Kareem Broadstone

That is a few inspirational stuff. For no reason knew that opinions could be that varied. Thanks for all the enthusiasm to provide such helpful information here.

Kareem Broadstone United States

9/3/2010 12:24:37 PM #

Isagenix

It is my great pleasure to visit your website and to enjoy your excellent post here. I like them very much. I can feel that you paid much attention to those articles, as all of them make sense and are very useful.  Thanks so much for sharing.

Isagenix United States

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.6.1.0
Theme by Mads Kristensen | Modified by Mooglegiant