How to fix Error 11605 on Windows Server 2025 / Windows 11

Troubleshooting Guide: SQL Server Error 11605 — “%S_MSG are not allowed at the top level”

Why This Error Happens

This error typically occurs when a T-SQL batch contains certain statements (such as XML schema collection operations, specific DDL commands, or language constructs) that SQL Server requires to be executed as the sole statement in a batch, but they are instead combined with other statements or nested within a block. It frequently surfaces after query generation tools, ORM frameworks, or copy-pasted scripts bundle multiple top-level-only commands together, or when a stored procedure/trigger body improperly wraps a restricted statement inside a BEGIN…END block, GO-separated batch, or conditional logic. Corrupted or outdated query templates in applications connecting to SQL Server 2025 can also trigger this behavior after an upgrade from older engine versions with looser parsing rules.

Step-by-Step Resolution Guide

Step 1: Identify the Offending Statement

  • Open SQL Server Management Studio (SSMS) or Azure Data Studio and locate the exact query that threw Error 11605.
  • Check the SQL Server error log at C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\Log\ERRORLOG for the full message, which will specify the %S_MSG placeholder (e.g., “CREATE/ALTER statements”).
  • Isolate the statement by running it alone in a new query window to confirm it is the trigger.

Step 2: Separate the Statement into Its Own Batch

  • Insert a GO keyword immediately before and after the restricted statement so it executes independently.
  • Remove the statement from any BEGIN...END, IF, WHILE, or TRY...CATCH blocks, since many top-level-only commands (like CREATE XML SCHEMA COLLECTION, certain ALTER DATABASE options, or USE statements inside procedures) cannot execute inside nested logic.
  • Example fix:
  GO
  CREATE XML SCHEMA COLLECTION dbo.MySchema AS N'<schema>...</schema>';
  GO

Step 3: Update Application or ORM-Generated Scripts

  • If the error originates from an application (e.g., Entity Framework, SSIS package, or a custom .NET service) deployed on Windows Server 2025, check the generated SQL script in the application’s logging output or SQL Profiler trace.
  • Patch the data access layer or migration script to issue the restricted command as a standalone ExecuteNonQuery() call rather than concatenating it with other SQL text.
  • Rebuild and redeploy the affected DLL or migration script after the fix.

Step 4: Validate Compatibility Level and Patch Level

  • Confirm the database compatibility level is correctly set for SQL Server 2025 by running:
  SELECT name, compatibility_level FROM sys.databases WHERE name = 'YourDatabase';
  • Apply the latest Cumulative Update (CU) for SQL Server 2025 via Microsoft Update or the SQL Server Installation Center, as parser-related bugs affecting batch separation are sometimes addressed in CUs.

Alternative or Verification Steps

  • Re-run the previously failing script or stored procedure in SSMS after applying the GO batch separation; a successful execution with no Error 11605 confirms the fix.
  • If the error persists, extract the procedure definition using sp_helptext 'YourProcedureName' and manually rewrite it using dynamic SQL (sp_executesql) to isolate the problematic statement into its own execution context.