Why This Error Happens
Error 2266 occurs when the SQL Server XML parser encounters a malformed XML document where an opening tag does not have a matching closing tag, or the closing tag uses an incorrect namespace prefix. This typically happens during OPENXML, sp_xml_preparedocument, or when inserting/casting data into an xml typed column where the source data was truncated, improperly encoded, or generated by an application that failed to close an element correctly. It can also surface after an in-place upgrade to SQL Server 2025 if legacy XML schema collections contain namespace mismatches that were previously tolerated by the older parser.
Step-by-Step Resolution Guide
Step 1: Isolate the Offending XML Payload
- Capture the exact XML string being passed into the failing query using SQL Server Profiler or Extended Events (
sqlserver.error_reportedfiltered on Error = 2266). - Copy the XML into a
.xmlfile and open it in a validator (Visual Studio, VS Code with XML extension, orxmllint.exe) to pinpoint the unclosed or mismatched tag. - Run
xmllint --noout yourfile.xmlfrom an elevated PowerShell prompt on Windows Server 2025 to get the exact line/column of the break.
Step 2: Correct the Source Data Generation Process
- Check the application or ETL job (SSIS, Python script, .NET service) generating the XML for truncation — commonly caused by a
varchar(max)being implicitly cast down tovarchar(8000)ornvarchar(4000)before insertion. - If using
FOR XML PATHorFOR XML AUTOto generate the document, verify that namespace prefixes declared withWITH XMLNAMESPACESmatch exactly between the root and child elements. - Example fix in T-SQL:
WITH XMLNAMESPACES ('http://schemas.example.com/ns' AS ns)
SELECT ... FOR XML PATH('ns:Root'), ELEMENTS
Step 3: Validate Against the XML Schema Collection
- If the column uses a typed XML schema collection, run the following to confirm the collection itself isn’t corrupted or mismatched post-upgrade:
SELECT name, xml_collection_id
FROM sys.xml_schema_collections
WHERE name = 'YourSchemaName';
- Re-validate the data against the schema manually:
DECLARE @x XML(YourSchemaCollection);
SET @x = '<ns:Root xmlns:ns="http://schemas.example.com/ns">...</ns:Root>';
- If this throws 2266, the schema collection’s namespace definition is out of sync with the data — drop and recreate the schema collection with
CREATE XML SCHEMA COLLECTIONusing the corrected.xsd.
Step 4: Check for Upgrade-Related Parser Behavior Changes
- Confirm the database compatibility level post-migration to SQL Server 2025:
SELECT name, compatibility_level FROM sys.databases WHERE name = 'YourDB';
- If the compatibility level is outdated (e.g., still at 140 or 150), some legacy XML handling quirks may not align with the newer, stricter parser. Update it:
ALTER DATABASE YourDB SET COMPATIBILITY_LEVEL = 170;
Alternative or Verification Steps
- Re-run the original query/procedure that threw Error 2266 after correcting the XML source; confirm no error is returned and
SELECT @x(if typed XML) returns the parsed document successfully. - As a quick workaround while the root cause is fixed upstream, wrap the XML insertion logic in a
TRY...CATCHblock withsp_xml_removedocumentcleanup and log the raw payload to a staging table for offline diagnosis rather than failing the entire transaction.