Disconnect all users and set a database to single-user
ALTER DATABASE statement that disconnects all other users at once by setting the database to SINGLE_USER with ROLLBACK IMMEDIATE.
To disconnect every other user from a database at once, set the database to single-user mode. ROLLBACK IMMEDIATE ends the other sessions and rolls back their open transactions instead of waiting for them to finish.
ALTER DATABASE <database_name>
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
Notes
- Rolled-back work is lost: the open transactions of the disconnected sessions are not committed.
- To let users connect again, set the database back to multi-user mode:
ALTER DATABASE <database_name> SET MULTI_USER;