I've got some code that adds new xml tag with the value of existing item to my XML MyColumn column in table MyTable.
UPDATE MyTable SET MyColumn.modify('insert element NewItem {/RootElement/ExistingItem/node()} after (/RootElement/ExistingItem)[1]')
What I want to do is edit this value presented as 开发者_JAVA百科xpath expression. Let's say I have value of "Test" in my ExistingItem element. And I want to have "something Test else" in my NewItem element.
What should I do?
Somewhat cumbersome solution, in case something simpler won't come up:
--sample data
declare @t table
(
col xml
)
insert into @t select '<RootElement><ExistingItem>Test</ExistingItem></RootElement>'
--solution
--read existing element into a variable
declare @str varchar(50)
select @str = 'something ' + cast(t.c.query('data(.)') as varchar) + ' else'
from @t cross apply col.nodes('(/RootElement/ExistingItem)[1]') t(c)
--insert new element with the value of the variable
update @t set col.modify('insert element NewItem {sql:variable("@str")} after (/RootElement/ExistingItem)[1]')
select * from @t
精彩评论