2011-10-13 27 views
0

我已经有了一些代码,它将现有项目的值的新xml标记添加到表MyTable的My MyColumn列中。更改xml列中标记的值

UPDATE SET MyTable的MyColumn.modify( '插入元件的newitem {/ rootElement的/ ExistingItem /节点()}后(/ rootElement的/ ExistingItem)[1]')

我想要做的是编辑该值呈现为xpath表达式。 假设我在ExistingItem元素中有“Test”的值。 我想在我的NewItem元素中有“测试其他东西”。

我该怎么办?

回答

1

有点麻烦的解决方案,如果简单的东西不上去:

--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