Troubleshooting Guide: SQL Server Error 2275 – “Unterminated XML Declaration”
Why This Error Happens
Error 2275 occurs when SQL Server’s XML parser encounters an XML declaration (<?xml version="1.0"?>) that is improperly closed or malformed within a column, parameter, or FOR XML/OPENXML operation. This is typically caused by corrupted or truncated XML data being inserted into an xml-typed column, improper string concatenation that splits the declaration tag, or encoding mismatches (e.g., UTF-16 vs UTF-8) during data import. It can also surface after an in-place upgrade to SQL Server 2025 if legacy XML data was stored using deprecated parsing leniencies no longer supported by the updated XML engine.
Step-by-Step Resolution Guide
Step 1: Identify the Offending Query or Data Source
- Check the SQL Server Error Log (
C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\Log\ERRORLOG) for the exact statement or stored procedure that triggered Error 2275. - Run
SELECT * FROM sys.dm_exec_query_statscross-referenced withsys.dm_exec_sql_textto pinpoint the query handle associated with the failure timestamp. - If the error originates from an application (e.g., .NET app on Windows 11 client), enable SQL Profiler or Extended Events to capture the exact XML payload being sent.
Step 2: Validate and Repair the XML Payload
- Extract the raw XML string from the source (file, API response, or database column) and validate it using a tool like
xmllint --noout file.xmlor Visual Studio’s XML Editor. - Look specifically for truncated declarations, such as
<?xml version="1.0missing the closing?>. - If the data is being concatenated in T-SQL, ensure you are not splitting the declaration tag:
SET @xmlData = '<?xml version="1.0" encoding="UTF-8"?>' + @BodyContent;
- If importing via
OPENROWSET(BULK...), verify theSINGLE_CLOBorSINGLE_BLOBoption matches the actual file encoding.
Step 3: Correct Encoding Mismatches
- Navigate to the file source properties (right-click file in Windows 11/Server 2025 Explorer > Properties) or check the HTTP header
Content-Typeof the data source. - Ensure the encoding declared in the XML header (e.g.,
encoding="UTF-8") matches the actual byte-order-mark (BOM) of the file. - In SQL Server Management Studio (SSMS), re-save the script or import file using the correct encoding via
File > Save As > Save with Encoding.
Step 4: Update SQL Server 2025 Cumulative Updates
- Open PowerShell as Administrator and check your current build version:
SELECT @@VERSION;
- Navigate to the Microsoft Update Catalog and apply the latest Cumulative Update (CU) for SQL Server 2025, as early releases had a known regression in the
sqllang.dllXML parsing module. - Restart the SQL Server service via
services.mscornet stop MSSQLSERVER && net start MSSQLSERVERafter patching.
Alternative or Verification Steps
- Verify the fix: Re-run the original INSERT/UPDATE statement or application transaction that triggered the error; confirm success by querying the table with
SELECT ISNULL(TRY_CAST(XmlColumn AS XML), 'Invalid') FROM YourTableto ensure no NULLs are returned for invalid casts. - Alternative workaround: If immediate resolution isn’t possible, wrap the insertion logic in a
TRY_CASTorTRY_CONVERTfunction to gracefully handle malformed XML and log rejected rows to a quarantine table instead of halting the transaction.