Troubleshooting Guide: SQL Server Error 9326 — “Computed comment constructors are not supported”
Why This Error Happens
This error occurs when SQL Server’s XML/XQuery parser encounters a comment constructor expression used in a computed context (such as within a dynamically built XQuery expression) that the engine’s XML processing layer cannot statically validate. SQL Server 2025 enforces stricter XQuery compliance checks, and computed comment nodes (comment { ... } built from variable or expression-driven content) are explicitly disallowed because they cannot be resolved into a static schema-valid node tree. This is typically triggered by application code, ORM-generated queries, or stored procedures that construct XML using FLWOR expressions or comment{} constructors with non-literal content.
Step-by-Step Resolution Guide
Step 1: Identify the Offending Query or Procedure
- Check the SQL Server Error Log and Windows Event Viewer (Applications and Services Logs > Microsoft > SQL Server) for the exact session ID and statement text tied to Error 9326.
- Run the following to capture the live offending batch:
SELECT text FROM sys.dm_exec_requests r
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle)
WHERE r.session_id = <SPID>;
- Look specifically for
FLWOR,.query(),.nodes(), orXML::comment{}constructs in the returned text.
Step 2: Replace Computed Comment Constructors with Static or Workaround Logic
- Rewrite any dynamic
comment { $variable }expressions as literal string concatenation instead, since SQL Server does not support computed comment nodes inside XQuery. - Example fix — instead of:
SET @x = @x.query('comment { sql:variable("@val") }')
Use string-based XML assembly:
SET @x = CAST('<!--' + @val + '-->' AS XML)
- Update any ORM-generated XML mapping templates (e.g., Entity Framework, Hibernate) that auto-generate comment nodes dynamically.
Step 3: Update SQL Server 2025 Cumulative Update
- Open SQL Server Configuration Manager or Windows Update on Windows Server 2025 and confirm you are on the latest Cumulative Update (CU) for SQL Server 2025, as early CU builds had incomplete XQuery validation messaging.
- Download the latest CU from the Microsoft Update Catalog and install via:
Start-Process "SQLServer2025-KB5XXXXXX-x64.exe" -ArgumentList "/quiet /IAcceptSQLServerLicenseTerms" -Wait
- Restart the SQL Server service (
services.msc> SQL Server (MSSQLSERVER) > Restart) after patching.
Step 4: Validate XML Schema Collections
- If the XML column uses a schema collection, verify it does not define elements expecting comment nodes:
SELECT name, xml_collection_id FROM sys.xml_schema_collections;
- Drop and recreate the schema collection if it references deprecated comment-node typing from older SQL Server versions (pre-2019 compatibility level).
Alternative or Verification Steps
- Re-run the original query/procedure after applying the string-based XML workaround and confirm no Error 9326 appears in
sys.dm_os_ring_buffersor the SQL error log. - If the error persists, set the database compatibility level back one tier (e.g., from 170 to 160) using
ALTER DATABASE [YourDB] SET COMPATIBILITY_LEVEL = 160;as a temporary mitigation while refactoring the XQuery logic.