0

I have a temp table that contains values I want injected as elements into a SQL Server column. I can do this for one entry using the modify and insert statements. But how can I do this for multiple rows in a table?

Some sample data looks like this:

SerializedTable:

ID    SerializedXML
-----------------------------------------
7     <Form> <Field1>111</Field1> </Form>
8     <Form> <Field1>112</Field1> </Form>

#TempTable:

ID    FK_ID    Value
--------------------
1     7        120
2     8        124

So I need to add a new element to SerializedXML with the values in the value column.

So the final table would look like:

SerializedTable:

ID    SerializedXML
---------------------------------------------------------------
7     <Form> <Field1>111</Field1> <Field2>120</Field2> </Form>
8     <Form> <Field1>112</Field1> <Field2>124</Field2> </Form>

The name of the element will be the same for all of the new inserted fields.

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
CorribView
  • 641
  • 1
  • 16
  • 41

1 Answers1

1

Try this

update s
set SerializedXML.modify('
  insert <Field2>{ sql:column("t.Value") }</Field2>
  after (/Form/Field1)[1] 
')
from SerializedTable s
join TempTable t on s.ID = t.FK_ID

SQL fiddle

Alexander Petrov
  • 12,244
  • 2
  • 16
  • 47