It is often necessary to change the database to single user mode, especially if you are a DBA. A simple example would be to change the collation settings or any DB settings. The single user will allow only one user ( usually DBA) to access the database. Hence it will be easy to make changes without the worry of deadlocks or any other type of contention for DB and also without affecting the users.
It is very easy to change the database to Single user mode; in fact, it is just an execution of the script away. Use the script below to change the mode.
ALTER DATABASE <> SET SINGLE_USER WITH NO_WAIT
The NO_WAIT clause will set it to single user mode as soon as you execute the query. Analternate to this is by using the system stored procedure sp_dboption
EXEC SP_DBOPTION <<Data base Name>>,‘SINGLE USER’ ,False
You need to however set the database back to multi user mode for other users to use it. Without that, it won’t be accessible by others.
Here is the script to set the database to multiuser mode.
ALTER DATABASE <> SET MULTI_USER WITH NO_WAIT
Or the alternate
EXEC SP_DBOPTION <>,‘SINGLE USER’ ,False
Maintain caution while changing database to single user mode, as it would disconnect the users already connected to the database.
Comments
Post a Comment