Troubleshooting Guide: SQL Server Error 2312 – XML Schema Attribute Type Mismatch
Why This Error Happens
Error 2312 occurs when SQL Server’s XML parser validates an XML document or instance against a registered XML Schema Collection (XSD) and finds that an attribute’s value does not match its declared data type (e.g., a non-numeric string assigned to an xs:integer or an invalid date format assigned to xs:dateTime). This is typically caused by malformed source data being inserted into a typed xml column, a mismatched or outdated XML Schema Collection definition, or data corruption introduced during an ETL/import process. It can also surface after an application update changes the XML payload structure without updating the corresponding schema collection in SQL Server 2025.
Step-by-Step Resolution Guide
Step 1: Identify the Offending XML Attribute and Value
- Capture the full error message text, including the
%lsplaceholders, which will reveal the exact attribute name, the expected XML Schema type (e.g.,dateTime,int,boolean), and the invalid value. - Run the failing
INSERTorUPDATEstatement in isolation using SQL Server Management Studio (SSMS) on Windows Server 2025 to reproduce the error in a controlled session. - Use
SELECT xml_column.query('.')on the staging data (if available) to inspect the raw XML before it is cast to the typed column.
Step 2: Validate the XML Schema Collection Definition
- Query the current schema collection definition to confirm expected data types:
SELECT xsc.name, xsc.xml_schema_namespace,
CAST(xml_schema_namespace AS xml) AS schema_definition
FROM sys.xml_schema_collections xsc
WHERE xsc.name = 'YourSchemaCollectionName';
- Compare the attribute type (e.g.,
xs:decimal,xs:boolean) in the schema against the actual value being passed in the XML payload. - Check for locale-related formatting issues, such as decimal separators (
,vs.) or date formats (MM/DD/YYYYvs ISO 8601YYYY-MM-DDTHH:MM:SS), which commonly cause type mismatches on Windows Server 2025 with regional settings differences.
Step 3: Correct the Source Data or Update the Schema Collection
- If the source application is sending malformed data, correct the data generation logic (e.g., ensure integers are not passed with leading zeros as strings, or booleans use
true/false/0/1strictly). - If the schema itself is outdated, alter the XML Schema Collection to reflect the correct type:
ALTER XML SCHEMA COLLECTION YourSchemaCollectionName ADD
'<xs:schema>... updated type definition ...</xs:schema>';
- Restart the SQL Server service via Services.msc or
net stop MSSQLSERVER && net start MSSQLSERVERif the schema cache appears stale after the update.
Step 4: Test with a Minimal XML Sample
- Strip the XML payload down to a single attribute/value pair that matches the error and test it against the schema collection using
sys.xml_schema_collectionsvalidation in a sandbox table. - Incrementally add back attributes until the error reproduces, isolating exactly which field is non-compliant.
Alternative or Verification Steps
- Re-run the original
INSERT/UPDATEstatement after correcting the data or schema; confirm no Error 2312 is raised and verify data integrity withSELECT * FROM YourTable WHERE xml_column IS NOT NULL. - If the issue persists intermittently, consider relaxing strict typing by temporarily using an untyped
xmlcolumn for ingestion, then validating and casting to the typed column only after a successfulTRY_CASTor schema validation pass in a staging procedure.