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
SET SINGLE_USER WITH ROLLBACK IMMEDIATEcancels all running transactions and closes open connections. This tends to speed things up -- at the obvious cost of interrupting whatever else was going on . - Jeroen Mostert