Troubleshooting Guide: SQL Server Error 5058 – “Option ‘%.ls’ cannot be set in database ‘%.ls'”
Why This Error Happens
This error typically occurs when attempting to modify a database option while the database is in a state that does not permit changes, such as being in use by other users, in SINGLE_USER mode with an active conflicting session, or involved in a database mirroring/Always On Availability Group configuration that restricts certain SET operations. It can also happen if the option being set is incompatible with the current database compatibility level or if the database is a system database (like master or tempdb) that has restrictions on certain ALTER DATABASE options. Additionally, insufficient permissions or the database being in RESTORING, OFFLINE, or EMERGENCY state can trigger this error.
Step-by-Step Resolution Guide
Step 1: Verify Database State and Active Connections
- Open SQL Server Management Studio (SSMS) or a query window and run
SELECT state_desc, user_access_desc FROM sys.databases WHERE name = 'YourDatabaseName';to check the current state. - Ensure the database is
ONLINEand notRESTORING,OFFLINE, orEMERGENCY. - If the database shows active connections blocking the change, run
SELECT * FROM sys.dm_exec_sessions WHERE database_id = DB_ID('YourDatabaseName');to identify open sessions.
Step 2: Set Database to Single-User Mode (If Required)
- Run the following command to force single-user access before applying the option change:
ALTER DATABASE YourDatabaseName SET SINGLE_USER WITH ROLLBACK IMMEDIATE;- This disconnects all other sessions and allows exclusive access for configuration changes.
- Proceed with your original
ALTER DATABASEcommand (e.g., settingAUTO_CLOSE,READ_COMMITTED_SNAPSHOT, etc.).
Step 3: Check for Availability Group or Mirroring Restrictions
- If the database is part of an Always On Availability Group, verify that you are connected to the Primary Replica.
- Run
SELECT role_desc FROM sys.dm_hadr_availability_replica_states WHERE is_local = 1;on the node to confirm its role. - Some options cannot be changed while mirroring or AG synchronization is active; you may need to temporarily remove the database from the AG/mirroring session before applying the option.
Step 4: Revert to Multi-User Mode and Confirm Compatibility Level
- Once the option has been successfully applied, return the database to multi-user access:
ALTER DATABASE YourDatabaseName SET MULTI_USER;- Confirm the database compatibility level supports the option using
SELECT compatibility_level FROM sys.databases WHERE name = 'YourDatabaseName';and update if necessary withALTER DATABASE YourDatabaseName SET COMPATIBILITY_LEVEL = 160;(adjust value for SQL Server 2025).
Alternative or Verification Steps
- To verify the fix, re-run the original
ALTER DATABASEstatement that previously failed; it should now execute without raising Error 5058. - If the issue persists, check the SQL Server Error Log (via
EXEC sp_readerrorlog;or Event Viewer under Windows Logs > Application) for additional context on blocking sessions or system-level restrictions preventing the change.