Why This Error Happens
Error 6923 occurs when SQL Server’s XML Schema Collection (XSD) validates an XML instance against a defined schema, and the parser encounters an element in the XML data that is not declared or expected at that position in the schema sequence. This typically happens due to a mismatch between the application’s XML output format and the schema registered in the database (often after a schema update) or because of incorrect element ordering, namespace mismatches, or an extra/optional field that wasn’t accounted for in the XSD definition. It can also surface after migrating databases to SQL Server 2025 if legacy XML Schema Collections weren’t updated to match new application payloads.
Step-by-Step Resolution Guide
Step 1: Identify the Offending XML and Schema Collection
- Run the failing query/insert statement in SQL Server Management Studio (SSMS) to capture the full error message, noting the exact
%s(unexpected element name) and%S_MSG(schema detail). - Query
sys.xml_schema_collectionsto locate the specific schema collection bound to the column:
SELECT * FROM sys.xml_schema_collections WHERE name = 'YourSchemaName'; - Export the current schema definition using
xml_schema_namespacefunction to compare it against the incoming XML payload.
Step 2: Compare XML Payload Against XSD Definition
- Open the incoming XML data (from the application log or
Profiler/Extended Eventstrace) and check the element causing the conflict. - Verify the sequence order inside the
<xs:sequence>tags in the XSD; SQL Server XML validation is strict about ordinal placement unless<xs:all>is used. - Check for namespace prefix mismatches (e.g.,
xmlns:ns1vsxmlns:ns2) that might cause the parser to flag a valid element as “unexpected.”
Step 3: Update or Alter the XML Schema Collection
- If the application is sending new, valid data fields, update the schema rather than hardcoding a bypass:
ALTER XML SCHEMA COLLECTION YourSchemaName ADD 'N<xs:schema>...</xs:schema>'; - If the element is truly invalid (e.g., a bug in the sending application), roll back the application’s API version or XML serializer settings to match the expected contract.
- Restart the SQL Server (MSSQLSERVER) service via
services.mscon Windows Server 2025 to clear any cached schema validation states if theALTERcommand does not take effect immediately.
Step 4: Check for SQL Server 2025 Compatibility Changes
- Verify the database compatibility level using
SELECT compatibility_level FROM sys.databases WHERE name = 'YourDB';. - If recently upgraded from an older engine, ensure compatibility level is set to
170(or the current 2025 level) to ensure the XML parser uses the correct validation engine:ALTER DATABASE YourDB SET COMPATIBILITY_LEVEL = 170;
Alternative or Verification Steps
- Verification: Re-run the original INSERT/UPDATE transaction that triggered Error 6923; if successful, check
sys.dm_exec_requeststo confirm no blocking or parsing errors remain in the active session. - Alternative: If modifying the production schema collection is too risky, create a pre-processing stored procedure that strips or validates optional unexpected nodes using
nodes()andvalue()XQuery methods before the data is inserted into the strictly-typed XML column.