Troubleshooting Guide: SQL Server Error 9313
Why This Error Happens
This error occurs when SQL Server 2025 parses an XML attribute constructor within a FOR XML query (or XQuery expression) that contains more than one expression, or an expression combined with literal string values, inside a single attribute value. The SQL Server XML parsing engine requires attribute constructors to resolve to a single, unambiguous expression value rather than a concatenated mix of variables, columns, and strings. This typically surfaces after a migration or upgrade when legacy T-SQL code containing non-compliant XQuery syntax is executed against the newer SQL Server 2025 engine, which enforces stricter XML standards compliance than previous versions.
Step-by-Step Resolution Guide
Step 1: Identify the Offending Query
- Capture the exact T-SQL statement that triggered Error 9313 using SQL Server Profiler or Extended Events (
xml_deadlock_reportwon’t help hereāuse a generic error event filter on Error Number 9313). - Review the query for
FOR XML PATHorFOR XML EXPLICITclauses, specifically looking at attribute definitions written like@attr = col1 + '-' + col2. - Check any recently migrated stored procedures located in your database’s Programmability > Stored Procedures node in SQL Server Management Studio (SSMS) 21.
Step 2: Rewrite the Attribute Constructor Using CONCAT()
- Replace mixed string/expression concatenation inside the attribute constructor with the
CONCAT()function so only a single resolved expression is passed. - Example fix: change
SELECT col1 + '_' + col2 AS "@AttributeName" FOR XML PATH('Root')toSELECT CONCAT(col1, '_', col2) AS "@AttributeName" FOR XML PATH('Root'). - Alternatively, pre-calculate the concatenated value in a derived table or Common Table Expression (CTE) before passing it into the
FOR XMLclause.
Step 3: Update Compatibility Level Settings
- Open SSMS and connect to the instance, then navigate to Database Properties > Options > Compatibility Level.
- Confirm the database is not unintentionally running under an older compatibility level (e.g., 150 or 160) that may interact unexpectedly with the new XML parser; set it to 170 for full SQL Server 2025 engine behavior using:
ALTER DATABASE [YourDB] SET COMPATIBILITY_LEVEL = 170; - Restart the SQL Server service via Services.msc on Windows Server 2025 if the setting does not immediately take effect.
Step 4: Validate XML Schema Collections
- If the attribute constructor is tied to a typed XML column, check the associated XML Schema Collection for outdated constraints using
SELECT * FROM sys.xml_schema_collections;. - Drop and recreate the schema collection if it references deprecated attribute typing rules from earlier SQL Server versions.
Alternative or Verification Steps
- Re-run the corrected query in SSMS and confirm the XML output generates successfully without Error 9313, then check the Windows Event Viewer (Applications and Services Logs > MSSQLSERVER) to confirm no recurrence.
- If the error persists on complex nested queries, isolate the
FOR XMLclause into a standalone temporary stored procedure to test theCONCAT()fix in isolation before merging it back into the production codebase.