简体   繁体   中英

SET Single_USER Mode SQL Server

I need help with setting a database in Single User Mode, Every time i run below code, But it's taking endless process

USE {Database_Name}

ALTER DATABASE {Database_Name} SET SINGLE_USER;
 GO
DBCC CHECKDB({Database_Name},REPAIR_REBUILD)
 GO
ALTER DATABASE {Database_Name} SET MULTI_USER;
 GO

Below SQL set the database to SINGLE_USER mode to obtain exclusive access. Initially It sets 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 database to be immediately disconnected .

Example SQL:

USE master
GO

ALTER DATABASE DB_Name
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
GO

DBCC CHECKDB('database_name', REPAIR_REBUILD)
GO

ALTER DATABASE DB_Name
SET READ_ONLY;
GO

If you want to set Database back to Multi_suer , use below SQL:

ALTER DATABASE DB_name
SET MULTI_USER;
GO

For more details check this link: How to set a Database to Single-user Mode

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM