1

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.

1 Answer 1

3

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

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

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.