Why This Error Happens
Error 115 occurs because SQL Server’s T-SQL syntax does not permit the FOR UPDATE clause to be combined with set operators such as UNION, UNION ALL, INTERSECT, or EXCEPT within the same statement. This is a parser-level restriction rather than a permissions or configuration issue — the query engine cannot determine cursor-updatability semantics when multiple result sets are being merged. It typically surfaces when legacy code (often ported from Oracle or Sybase) or auto-generated ORM queries attempt to declare an updatable cursor over a combined result set.
Step-by-Step Resolution Guide
Step 1: Identify the Offending Query
- Locate the exact T-SQL batch referenced in the error message using SQL Server Management Studio (SSMS) or Azure Data Studio.
- Check the
DECLARE CURSORstatement for aFOR UPDATEclause combined withUNION,UNION ALL,INTERSECT, orEXCEPT. - Run the query in isolation to confirm Error 115 is reproducible outside of the application layer.
Step 2: Refactor the Cursor Logic
- Split the query into separate
DECLARE CURSOR ... FOR UPDATEstatements, one per underlying table involved in the set operation. - Alternatively, remove the
FOR UPDATEclause and instead perform updates using aWHERE CURRENT OFpattern against a single-table subquery, or switch to a read-only cursor combined with a separateUPDATE ... WHEREstatement based on the retrieved key values. - If the intent was simply to filter and then modify rows, replace the cursor-based logic with a set-based
UPDATEstatement using aJOINorMERGEinstead ofUNION.
Step 3: Update Application or ORM-Generated Code
- Review any Entity Framework, Hibernate, or legacy Sybase/Oracle migration scripts that auto-generate cursor syntax, since these often produce invalid
FOR UPDATE+ set operator combinations. - Manually patch the generated SQL template or add a custom query override in the data access layer to avoid combining set operators with updatable cursors.
- Save the corrected script to
C:\Scripts\SQL\Fixes\(or your team’s standard repository path) for version control and future audits.
Step 4: Test in a Non-Production Environment
- Deploy the corrected script to a staging instance of SQL Server 2025 running on Windows Server 2025 or Windows 11.
- Execute the batch via
sqlcmd -S <ServerName> -i FixedScript.sqlto confirm no Error 115 is thrown. - Monitor the SQL Server Error Log (
Management > SQL Server Logsin SSMS) for any residual syntax warnings.
Alternative or Verification Steps
- Verify the fix by re-running the original failing procedure or application transaction and confirming that no Error 115 appears in the SQL Server Error Log or client application output.
- As an alternative, if refactoring the cursor is not feasible short-term, wrap the set-operation query in a temporary table or table variable first, then declare the
FOR UPDATEcursor against that single temporary object instead of the combined result set directly.