示例
declare @x xml;
set @x = N'
<Root>
<Book>
<ID>1</ID>
<Price>100</Price>
<Title>false</Title>
</Book>
<Book>
<ID>2</ID>
<Price>200</Price>
<Price>false</Price>
</Book>
<Book>
<ID>3</ID>
<Price>300</Price>
<Title>false</Title>
</Book>
<Book>
<ID>4</ID>
<Price>400</Price>
<Title>false</Title>
</Book>
</Root>'
查询以选择包含id = 3
的子节点的节点SELECT
@tmp = a.b.query('../.')
from @x.nodes('//Book/ID[text()="3"]') a(b)
如何修改所选节点的价格值?
答案 0 :(得分:2)
考虑使用SQL Server的XML Data Modification Language (DML),特别是replace value of方法。以下示例将Price
更新为9999:
declare @x xml;
set @x = N'
<Root>
<Book>
<ID>1</ID>
<Price>100</Price>
<Title>false</Title>
</Book>
<Book>
<ID>2</ID>
<Price>200</Price>
<Price>false</Price>
</Book>
<Book>
<ID>3</ID>
<Price>300</Price>
<Title>false</Title>
</Book>
<Book>
<ID>4</ID>
<Price>400</Price>
<Title>false</Title>
</Book>
</Root>'
set @x.modify('replace value of (/Root/Book[ID="3"]/Price/text())[1] with ("9999")')
select @x