Troubleshooting Guide: SQL Server Error 2272 – “%s’?>’ expected”
Why This Error Happens
Error 2272 occurs when SQL Server’s XML parser encounters malformed XML syntax, typically due to an unclosed or improperly terminated processing instruction (such as a missing ?> closing tag). This is commonly caused by corrupted XML data being passed into FOR XML queries, OPENXML functions, or XML-typed columns, or by truncated XML content resulting from incomplete data imports or application-side string handling errors. In Microsoft SQL Server 2025 running on Windows Server 2025 or Windows 11, this can also surface after collation or encoding mismatches silently alter special characters within XML payloads.
Step-by-Step Resolution Guide
Step 1: Identify the Offending Query or Data Source
- Check the SQL Server error log located at
C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\Log\ERRORLOGfor the full stack trace and query context. - Run
SELECT * FROM sys.dm_exec_query_statscross-referenced withsys.dm_exec_sql_textto identify the exact statement that triggered the error. - Isolate whether the error originates from an application-generated XML string, an SSIS package, or a stored procedure using
FOR XML PATH.
Step 2: Validate and Repair the XML Payload
- Extract the problematic XML string and validate it using
SELECT CAST(@XmlString AS XML);in a test query window to reproduce the error in isolation. - Inspect the raw string for truncation, especially at the end where
?>should appear—look for cut-off content due toVARCHARlength limits (e.g., casting toVARCHAR(MAX)instead ofVARCHAR(8000)). - If the source is an external file, open it in a text editor like Notepad++ and verify the XML declaration line reads correctly as
<?xml version="1.0" encoding="UTF-8"?>.
Step 3: Correct Encoding and Collation Settings
- Verify the database collation using
SELECT DATABASEPROPERTYEX('YourDBName', 'Collation');and ensure it matches the expected character set of the incoming XML data. - If data originates from an external ETL process, confirm the connection string or SSIS Data Flow Task uses UTF-8 or UTF-16 encoding consistently to avoid character corruption.
- Update application code or import scripts to properly escape special characters (
&,<,>) before constructing XML strings.
Step 4: Apply SQL Server 2025 Cumulative Update
- Open SQL Server Configuration Manager on Windows Server 2025 and confirm the current build number via
SELECT @@VERSION;. - Navigate to the Microsoft Update Catalog and download the latest Cumulative Update (CU) for SQL Server 2025, as XML parser edge-case bugs are frequently patched in CUs.
- Install the update and restart the SQL Server service via
services.mscornet stop MSSQLSERVER && net start MSSQLSERVER.
Alternative or Verification Steps
- Re-run the original query or application process that triggered Error 2272 and confirm successful execution without parser exceptions.
- As an alternative, wrap XML casting operations in a
TRY_CASTorTRY_CONVERTblock to gracefully handle malformed XML and log the failure for further review instead of crashing the transaction.