Troubleshooting Guide: SQL Server Error 6621 — XML Encoding/Decoding Error
Why This Error Happens
Error 6621 occurs when SQL Server’s XML parser encounters malformed, corrupted, or improperly encoded XML data while attempting to read or write an xml type object. This is typically caused by invalid Unicode character sequences, mismatched character encoding (such as UTF-8 data being interpreted as UTF-16 or vice versa), or physical corruption of the underlying data page storing the XML value. It can also surface when a column or variable of type xml is populated from an external source (file, API, or linked server) that injected unsupported control characters or an invalid byte-order mark (BOM).
Step-by-Step Resolution Guide
Step 1: Identify the Affected Object and Query Context
- Run the failing query in SSMS and capture the full error message, noting the object name returned in the
%.*lsplaceholder. - Use
SELECT OBJECT_NAME(object_id), name FROM sys.columns WHERE system_type_id = (SELECT system_type_id FROM sys.types WHERE name = 'xml')to locate all XML-typed columns in the suspect table. - Check SQL Server error logs at
C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\Log\ERRORLOGfor correlating entries with timestamps.
Step 2: Validate and Repair the XML Data Source
- If data is inserted via
OPENROWSET(BULK...)orBULK INSERT, confirm the source file’s encoding matches the expected format (UTF-8 without BOM is preferred for SQL Server 2025). - Re-export the source XML file using Notepad++ or PowerShell’s
Get-Content -Encoding UTF8to strip invalid BOM markers or control characters. - Run
SELECT CAST(xml_column AS NVARCHAR(MAX)) FROM YourTable WHERE id = <suspect_row>to manually inspect the raw string for embedded null bytes or invalid entities.
Step 3: Check for Database or Page-Level Corruption
- Execute
DBCC CHECKDB('YourDatabaseName') WITH NO_INFOMSGS, ALL_ERRORMSGSto rule out physical page corruption on the XML data pages. - If corruption is confirmed, restore the affected table or database from the last known good backup using
RESTORE DATABASE YourDatabaseName FROM DISK = 'D:\Backups\YourDatabaseName.bak' WITH REPLACE. - Review Windows Server 2025 Event Viewer under Windows Logs > System for disk I/O warnings (Event ID 51 or 153) that may indicate storage-layer issues causing the corruption.
Step 4: Rebuild Application-Side XML Generation Logic
- If the XML is generated by an application (e.g., via
FOR XML PATHor a .NET serializer), ensure the application explicitly sets encoding toUTF-8orUTF-16consistently before sending data to SQL Server. - Update any outdated XML libraries (such as older
System.Xmlversions in .NET Framework apps) to current .NET 8/9 equivalents that comply with stricter XML 1.0 encoding standards.
Alternative or Verification Steps
- After applying fixes, re-run the original query or stored procedure and confirm Error 6621 no longer appears; additionally run
DBCC CHECKDBonce more to confirm zero allocation or consistency errors. - As an alternative, if the XML column is non-critical, temporarily cast the data to
NVARCHAR(MAX)for storage and validate/re-cast it back toXMLonly after passing it throughdbo.fn_ValidateXML()(a custom validation function) to catch malformed data before insertion.