Why This Error Happens
This error occurs when an XML Data Modification Language (DML) statement—such as insert, delete, or replace value of—attempts to target a node that was generated dynamically (e.g., through a nodes() function result that was already transformed, a derived table, or a computed expression) rather than a node that exists persistently in an xml-typed column. SQL Server’s XQuery engine requires a direct, stable reference to the underlying storage node in order to apply a mutation, and a “constructed” node (one created in memory via a function or expression) has no addressable location to write back to. This typically surfaces after application updates, ORM-generated queries, or migration scripts that use nested .query() or .nodes() calls incorrectly in UPDATE statements.
Step-by-Step Resolution Guide
Step 1: Identify the Offending XML DML Statement
- Enable statement-level logging or check SQL Server Profiler/Extended Events for the exact query raising Error 2359.
- Look specifically for
.modify('insert ...'),.modify('delete ...'), or.modify('replace value of ...')calls in stored procedures or application code. - Confirm the target path inside the
modify()call references a column directly, not a subquery result.
Step 2: Correct the XQuery Target Reference
- Rewrite the query so the
modify()method is called directly against thexmlcolumn in the base table, not against an aliased or joined derived table. - Example of the problem:
UPDATE t
SET t.XmlCol.modify('insert <Node/> into (/Root)[1]')
FROM (SELECT XmlCol FROM MyTable) AS t
- Example of the fix:
UPDATE MyTable
SET XmlCol.modify('insert <Node/> into (/Root)[1]')
WHERE PrimaryKeyID = @ID
- Ensure the
WHEREclause isolates the exact row(s) using the primary key rather than filtering through a joined or computed source.
Step 3: Validate XML Schema Collection Compatibility
- If the column uses a typed XML schema collection, run
sys.xml_schema_collectionsto confirm the schema matches the structure being inserted/modified. - Drop and recreate the schema collection binding if it was altered during a recent SQL Server 2025 upgrade:
ALTER TABLE MyTable
ALTER COLUMN XmlCol XML (SchemaCollectionName)
Step 4: Test in Isolation on Windows Server 2025
- Reproduce the failing statement in SQL Server Management Studio (SSMS) or Azure Data Studio directly on the Windows Server 2025 host, bypassing the application layer.
- Confirm the SQL Server service account has appropriate
ALTERandUPDATEpermissions on the target table (check viasys.fn_my_permissions('MyTable', 'OBJECT')).
Alternative or Verification Steps
- Re-run the corrected
UPDATE/.modify()statement and confirm it completes without Error 2359; verify data integrity afterward withSELECT XmlCol FROM MyTable WHERE PrimaryKeyID = @ID. - If the issue persists, convert the operation to a two-step process: first
SELECTthe XML into a variable, run the XQuery.modify()against the local variable, thenUPDATEthe table using the fully modified variable value.