Why This Error Happens
Error 5062 occurs when a T-SQL statement—typically ALTER DATABASE or CREATE DATABASE—attempts to apply two mutually exclusive options in the same command, such as combining READ_ONLY with READ_WRITE, or SINGLE_USER with MULTI_USER. SQL Server 2025’s parser validates option compatibility before execution, and if conflicting flags are detected in the same batch, it immediately halts and throws this error. This is almost always caused by a scripting error, a legacy migration script written for an older SQL Server version, or an automated deployment tool (like a DACPAC or DevOps pipeline) passing duplicate/conflicting parameters.
Step-by-Step Resolution Guide
Step 1: Identify the Exact Conflicting Options
- Open the full error message in SQL Server Management Studio (SSMS) or the Windows Event Viewer (Event ID 5062) to see which two options are named in the
%.*lsplaceholders. - Common conflicting pairs include:
READ_ONLYvsREAD_WRITE,SINGLE_USERvsMULTI_USER,OFFLINEvsONLINE, andEMERGENCYvsONLINE. - Copy the exact T-SQL statement from the query window, SQL Agent job step, or deployment script that triggered the error.
Step 2: Review and Correct the T-SQL Script
- Open the script in SSMS and locate the
ALTER DATABASE [YourDB] SET ...line. - Remove one of the two conflicting clauses—keep only the option you actually intend to apply.
- Example fix: Change
ALTER DATABASE SalesDB SET READ_ONLY, READ_WRITEto simplyALTER DATABASE SalesDB SET READ_ONLY. - If the conflict originates from a CI/CD pipeline (Azure DevOps, DACPAC publish profile), check the
.publish.xmlor deployment contributor settings for duplicate option flags.
Step 3: Check for Automated Jobs or Maintenance Plans
- Open SQL Server Agent (via SSMS Object Explorer) and inspect any jobs that run
ALTER DATABASEcommands, especially those tied to maintenance plans or backup/restore automation. - On Windows Server 2025, verify the Task Scheduler (
taskschd.msc) isn’t running a duplicate PowerShell script (Invoke-Sqlcmd) with conflicting parameters against the same database. - Disable or edit the conflicting job step rather than deleting it, so you preserve an audit trail.
Step 4: Apply the Corrected Statement
- Re-run the corrected
ALTER DATABASEstatement in a new SSMS query window connected withsysadminordbcreatorprivileges. - Confirm no other session is holding a lock on the database by running:
SELECT request_session_id, resource_type, request_mode
FROM sys.dm_tran_locks
WHERE resource_database_id = DB_ID('YourDB');
- Execute the statement in single-user mode if the database is in use by other connections.
Alternative or Verification Steps
- Run
SELECT name, state_desc, is_read_only, user_access_desc FROM sys.databases WHERE name = 'YourDB';to confirm the database now reflects the intended state without errors. - If the error persists, restart the SQL Server service via
services.mscorRestart-Service MSSQLSERVER(PowerShell, run as Administrator) to clear any cached session state, then re-apply the corrected script.