SQL Server error 902 occurs when a database operation requires the database to be in a specific state—most commonly, when you try to change database settings (like recovery model or containment type) while the database is in use or not properly prepared. This error typically appears as "The database must be in single-user mode" or similar phrasing, depending on the operation. The fix involves placing the database in single-user mode, performing the required change, and restoring multi-user access. You need SQL Server Management Studio (SSMS) or Azure Data Studio, sysadmin privileges, and confirmation that no critical processes are running on the database.

What You'll Need

  • SQL Server Management Studio (SSMS) or Azure Data Studio installed
  • sysadmin server role membership or equivalent permissions
  • Access to the SQL Server instance hosting the target database
  • Confirmation that all non-essential connections to the database are closed (including your own SSMS query windows)

Step-by-Step

  1. Close all active connections to the database: In SSMS, expand Databases, right-click the target database, and select Properties. Go to the Options page. Note the current Restrict Access setting. Then, in a new query window, run the following command to kill all user connections (replace YourDatabaseName with the actual name):

    ALTER DATABASE [YourDatabaseName] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;

    This terminates all existing connections and forces the database into single-user mode. If you receive a message about the current session being blocked, close and reopen SSMS, reconnect, and rerun the command.
  2. Perform the required database change: Now that the database is in single-user mode, execute the operation that previously triggered error 902. For example, to change the recovery model to SIMPLE, run:

    ALTER DATABASE [YourDatabaseName] SET RECOVERY SIMPLE;

    Or to enable containment:

    ALTER DATABASE [YourDatabaseName] SET CONTAINMENT = PARTIAL;

    Ensure the command completes without error before proceeding.
  3. Restore multi-user access: After the operation succeeds, immediately switch the database back to multi-user mode to allow normal access. Run:

    ALTER DATABASE [YourDatabaseName] SET MULTI_USER;

    This step is critical—leaving a database in single-user mode can cause disruption for other users and applications.
  4. Verify the change and mode: Confirm the change took effect and the database is now in multi-user mode. In SSMS, right-click the database > Properties > Options and check the Restrict Access field (should show MULTI_USER). Also, re-run the original command to validate (e.g., check SELECT recovery_model_desc FROM sys.databases WHERE name = 'YourDatabaseName' for recovery model).

Common Mistakes

  • Running the SET SINGLE_USER command while still connected in the same SSMS query window—this leaves your session as the sole connection, but any subsequent query window or tool will be blocked. Always close SSMS and reconnect before proceeding.
  • Forgetting to run SET MULTI_USER afterward, leaving the database inaccessible to all but one user (often the sysadmin), which can cause application failures.
  • Attempting operations like restoring or detaching while the database is still in multi-user mode—these require exclusive access and will trigger error 902 if users are connected.
  • Using WITH NO_WAIT in SET SINGLE_USER when active connections exist—this causes the command to fail immediately instead of rolling back, wasting time and leaving the database in an inconsistent state.

Tips

  • Run KILL SPID manually only if SET SINGLE_USER WITH ROLLBACK IMMEDIATE fails—use SELECT session_id, login_name, host_name FROM sys.dm_exec_sessions WHERE database_id = DB_ID('YourDatabaseName') to find SPIDs, then KILL <SPID>.
  • Schedule maintenance during off-peak hours to minimize disruption and reduce the chance of missing active connections.
  • Use a maintenance plan or script that includes SET SINGLE_USER, your change, and SET MULTI_USER in sequence, wrapped in a transaction block where possible for atomicity.
  • Test the sequence on a non-production database first, especially when changing containment or recovery model, as these affect backup/restore behavior and security boundaries.

Troubleshooting

If error 902 persists or the database remains stuck in single-user mode:

  1. Check for orphaned connections: Run SELECT * FROM sys.dm_exec_sessions WHERE database_id = DB_ID('YourDatabaseName') AND is_user_process = 1. If any rows return, use KILL <session_id> to terminate them manually.
  2. Verify the database is not suspect or recovery-pending: In SSMS, expand Databases and check the status icon (a red X or yellow triangle). If present, run DBCC CHECKDB ('YourDatabaseName') and address any corruption errors before retrying.
  3. Restart the SQL Server service: As a last resort before escalating, stop and restart the SQL Server (MSSQLSERVER) Windows service. This clears all connections and may resolve stuck locks. Note: This will briefly interrupt all databases on the instance.
  4. Use Dedicated Administrator Connection (DAC): If normal connections fail, connect via DAC using ADMIN:<server_name> in SSMS, then run ALTER DATABASE [YourDatabaseName] SET MULTI_USER. DAC is reserved for administrators and only allows one connection.
  5. Review SQL Server logs: In SSMS, expand Management > SQL Server Logs, and check the most recent log for additional context around the error. Look for messages about locks, deadlocks, or transaction rollbacks that may explain why the operation stalled.

Frequently Asked Questions

Why does error 902 say "the database must be in single-user mode"?

Some operations—like changing the recovery model, enabling containment, or performing certain restores—require exclusive database access to prevent data inconsistency. SQL Server enforces this by requiring the database to be in single-user mode first.

Can I avoid using single-user mode for recovery model changes?

No. Changing the recovery model from FULL or BULK_LOGGED to SIMPLE requires exclusive access. There is no online alternative; single-user mode is the only supported method.

Will setting the database to single-user mode cause data loss?

No. The SET SINGLE_USER command does not alter data—it only changes connection permissions. The WITH ROLLBACK IMMEDIATE option terminates active transactions, which may cause uncommitted work to roll back, but committed data remains intact.

What if I lose connection after running SET SINGLE_USER?

Reconnect to SSMS (or Azure Data Studio) and immediately run ALTER DATABASE [YourDatabaseName] SET MULTI_USER. If you cannot connect at all, use the Dedicated Administrator Connection (DAC) as described in the troubleshooting section.

Does this work for Azure SQL Database?

No. Azure SQL Database handles connection management differently and does not support SET SINGLE_USER or SET MULTI_USER. For Azure SQL, use the ALTER DATABASE ... SET READ_WRITE or SET READ_ONLY commands as needed, but most settings changes require using the Azure portal or PowerShell/CLI with specific constraints.

Can I automate this fix in a SQL Agent job?

Yes. Create a job step that runs ALTER DATABASE ... SET SINGLE_USER WITH ROLLBACK IMMEDIATE, followed by your change, then ALTER DATABASE ... SET MULTI_USER. Include error handling (TRY/CATCH) to ensure SET MULTI_USER runs even if the middle step fails. However, test thoroughly—automated jobs that leave a database in single-user mode can cause service outages.