Troubleshooting Guide: SQL Server Error 2357 – “A document node may only be replaced with another document node”
Why This Error Happens
This error occurs when an application or T-SQL script attempts to use the modify() method of the xml data type to replace a top-level document node with a non-document node (such as a fragment or element without a single root). SQL Server’s XML schema validation enforces strict hierarchy rules, and the XQuery replace value of expression fails when the target and replacement node types do not match structurally. This is typically caused by malformed XML modification logic in stored procedures, corrupted XML column data, or an outdated application layer generating improperly structured XML fragments.
Step-by-Step Resolution Guide
Step 1: Identify the Offending Query or Stored Procedure
- Enable SQL Server Profiler or Extended Events to capture the exact T-SQL statement triggering Error 2357.
- Filter for
Error Number = 2357under the Errors and Warnings event category. - Note the associated
SPID, stored procedure name, and the XML column involved.
Step 2: Review and Correct the XML .modify() Syntax
- Open the stored procedure or script in SQL Server Management Studio (SSMS) 21 (compatible with SQL Server 2025).
- Check the
replace value ofclause — ensure you are not targeting(/*)[1](the document node) with a replacement that isn’t also a full document node. - Correct example:
SET @xmlData.modify('replace value of (/root/element/text())[1] with "NewValue"')
- Avoid replacing
(/)[1]directly; target a specific child node instead.
Step 3: Validate XML Schema Collections (if XSD-bound)
- Run the following to check if the column is bound to an XML Schema Collection:
SELECT c.name, xsc.name AS SchemaCollection
FROM sys.columns c
JOIN sys.xml_schema_collections xsc
ON c.xml_collection_id = xsc.xml_collection_id
WHERE c.object_id = OBJECT_ID('YourTableName');
- If a schema collection exists, confirm the replacement node structure matches the XSD-defined root element exactly.
Step 4: Rebuild or Repair Corrupted XML Data
- Run
DBCC CHECKDB('YourDatabaseName') WITH NO_INFOMSGS, ALL_ERRORMSGS;to rule out underlying page-level corruption affecting the XML binary large object (BLOB). - If corruption is found, restore the affected table/database from the last known good backup via
RESTORE DATABASE ... WITH RECOVERY.
Step 5: Apply Latest SQL Server 2025 Cumulative Update
- Navigate to Microsoft Update Catalog and confirm you are on the latest Cumulative Update (CU) for SQL Server 2025.
- On Windows Server 2025, verify via:
SELECT @@VERSION;
- Install any pending CU through SQL Server Setup → Maintenance → Edition Upgrade/Patch to ensure known XQuery engine bugs are patched.
Alternative or Verification Steps
- Re-run the original query in a test environment (non-production) using
SET STATISTICS XML ONto confirm the.modify()call executes without raising Error 2357. - If the issue persists after code correction, isolate the XML column into a temporary untyped
xmlvariable (remove schema binding temporarily) to confirm whether the XSD schema collection is the root cause.