Sql server 2005 如何使用XQuery更改匹配的元素文本?
我试图拼凑所有我见过的查找元素和修改它们的例子,但我还没有想出一些有效的方法。我在下面创建了一个到目前为止的示例,我正在SQLServer2005中运行。我正在尝试将ItemID 4更改为999:Sql server 2005 如何使用XQuery更改匹配的元素文本?,sql-server-2005,element,xquery,Sql Server 2005,Element,Xquery,我试图拼凑所有我见过的查找元素和修改它们的例子,但我还没有想出一些有效的方法。我在下面创建了一个到目前为止的示例,我正在SQLServer2005中运行。我正在尝试将ItemID 4更改为999: DECLARE @x XML SELECT @x = ' <ValueCollection> <ItemGroup Name="Group A"> <ItemID>1</ItemID> <ItemID>2</Item
DECLARE @x XML
SELECT @x = '
<ValueCollection>
<ItemGroup Name="Group A">
<ItemID>1</ItemID>
<ItemID>2</ItemID>
</ItemGroup>
<ItemGroup Name="Group B">
<ItemID>3</ItemID>
<ItemID>4</ItemID>
</ItemGroup>
</ValueCollection>
';
SET @x.modify ('
replace value of
(/ValueCollection/ItemGroup[ItemID="4"]/ItemID/text())[1]
with "999"
')
SELECT @x;
DECLARE@xxml
选择@x=
1.
2.
3.
4.
';
SET@x.modify('
替代价值
(/ValueCollection/ItemGroup[ItemID=“4”]/ItemID/text())[1]
加上“999”
')
选择@x;
但是现在发生的事情是ItemID 3变为999,而不是4,如下面的结果所示。我认为发生的是“/ItemGroup[ItemID=“4”]正在定位正确的位置,然后位置过滤器[1]给出了第一个位置过滤器(即ID 3)——我只是没有找到如何将位置过滤器更改为变量
<ValueCollection>
<ItemGroup Name="Group A">
<ItemID>1</ItemID>
<ItemID>2</ItemID>
</ItemGroup>
<ItemGroup Name="Group B">
<ItemID>999</ItemID>
<ItemID>4</ItemID>
</ItemGroup>
</ValueCollection>
1.
2.
999
4.
我感觉很简单,我的谷歌搜索和堆栈溢出还没有出现。我感谢你的帮助!凯文 或者,如果您想更精确一些:
SET @x.modify ('replace value of
(/ValueCollection/ItemGroup[@Name="Group B"]/ItemID[text()=4]/text())[1]
with "999"
')
SET @x.modify ('replace value of
(/ValueCollection/ItemGroup[@Name="Group B"]/ItemID[text()=4]/text())[1]
with "999"
')