簡體   English   中英

更改xml列中標記的值

[英]change the value of tag in xml column

我有一些代碼將帶有現有項目值的新xml標記添加到表MyTable的XML MyColumn列中。

更新MyTable SET MyColumn.modify('在(/ RootElement / ExistingItem)[1]之后插入元素NewItem {/ RootElement / ExistingItem / node()}

我想做的就是編輯以xpath表達式表示的該值。 假設我在ExistingItem元素中具有“ Test”的值。 我想在NewItem元素中進行“其他測試”。

我該怎么辦?

某種繁瑣的解決方案,以防出現更簡單的情況:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM