sql server - Insert Node as parent -


i have xml column content this

<root>   <element>     <tomove/>   </element>   <element>     <envelope>       <good>somevalue</good>     </envelope>   </element>  <element>     <tomove/>  </element>  <element>     <envelope>       <good>somevalue</good>     </envelope>   </element> </root> 

i want add new node envelope between element , tomove. (element/envelope/tomove) using xquery.

i tried adding envelope/tomove sibling tomove insert not support adding multiple nodes. adding envelope alone , adding tomove in next statement not seem possible since there envelope nodes should not tomove node.

any ideas?

edit: order , number of element nodes variable.

this might you. comments in code describes do.

-- setup test data 2 records 4 elements in each declare @records table(id int, content xml) insert @records values (1, '<root>        <element>          <tomove/>        </element>        <element>          <envelope>            <good>somevalue 1</good>          </envelope>        </element>       <element>          <tomove/>       </element>       <element>          <envelope>            <good>somevalue 2</good>          </envelope>        </element>      </root>'), (2, '<root>        <element>          <tomove/>        </element>        <element>          <envelope>            <good>somevalue 3</good>          </envelope>        </element>       <element>          <tomove/>       </element>       <element>          <envelope>            <good>somevalue 4</good>          </envelope>        </element>      </root>')  -- split elements, 1 row each @t declare @t table (id int, element xml)  insert @t select   id,   r.query('.') @records   cross apply content.nodes('root/element') r(r)  -- insert envelop/tomove there exist tomove update @t set element.modify('insert <envelope><tomove/></envelope> (element[1])') element.exist('element/tomove') = 1  -- remove tomove element node update @t set element.modify('delete element/tomove')  -- save changes @records, recombine elements update @records set content =    (     select (select t.element)      @t t     t.id = r.id     xml path('root')   ) @records r  --result select * @records 

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -