2

I have a column in sql database with type XML. I want to add a new node EFDRow with same value in all nodes with name GoodsSerial.

<Serials>
  <GoodsSerial>
    <GSerialSCredit>2015/08/11</GSerialSCredit>
    <GSerialECredit>2015/08/11</GSerialECredit>
    <GSerialStart>1</GSerialStart>
    <GSerialEnd>5</GSerialEnd>
  </GoodsSerial>
  <GoodsSerial>
    <GSerialSCredit>2015/08/11</GSerialSCredit>
    <GSerialECredit>2015/08/11</GSerialECredit>
    <GSerialStart>10</GSerialStart>
    <GSerialEnd>15</GSerialEnd>
  </GoodsSerial>
</Serials>

I have tried this Code

UPDATE Tbl
SET SerialXml.modify('insert <EFDRow>{sql:column("EFDRow")}</EFDRow> into (/Serials/GoodsSerial)[1]')

But it adds the EFDRow node only to first GoodsSerial node.

2
  • its not clearly understand. Can you show, what will be the output? update your que. Commented Aug 11, 2015 at 9:59
  • possible duplicate of xml-sql: update multiple nodes Commented Aug 11, 2015 at 11:22

2 Answers 2

2

You can shred your XML no /Serials/GoodsSerial and the rebuild it with the value of EFDRow added using for xml path.

update dbo.Tbl
set SerialXml = (
                select GS.X.query('*'),
                       EFDRow
                from SerialXml.nodes('/Serials/GoodsSerial') as GS(X)
                for xml path('GoodsSerial'), root('Serials'), type
                );

GS.X.query('*') will give you all the nodes that already exist in GoodsSerial.

SQL Fiddle

Sign up to request clarification or add additional context in comments.

Comments

0

Modify can only replace 1 value at a time. You must loop through all node:

Updating all nodes by condition with xml to sql

xml-sql: update multiple nodes

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.