Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何通过单个查询更新多个xml节点值_Sql_Sql Server_Xml_Sql Server 2008 - Fatal编程技术网

Sql 如何通过单个查询更新多个xml节点值

Sql 如何通过单个查询更新多个xml节点值,sql,sql-server,xml,sql-server-2008,Sql,Sql Server,Xml,Sql Server 2008,以xml为例 <root> <parent id="1"> <child> <field name="1"> <value>abc</value> </field> <field name="2"> <value>cdf</value> </field> <

以xml为例

<root>
  <parent id="1">
    <child>
      <field name="1">
        <value>abc</value>
      </field>
      <field name="2">
        <value>cdf</value>
      </field>
      <field name="2">
        <value>xyz</value>
      </field>
      <field name="1">
         <value>uvw</value>
      </field>
    </child>
  </parent>
  <parent id="2">
    <child>
      <field name="1">
        <value>123</value>
      </field>
      <field name="3">
        <value>234</value>
      </field>
      <field name="4">
        <value>34</value>
      </field>
      <field name="1">
        <value>544</value>
      </field>
   </child>
 </parent>

这适用于所有事件还是仅适用于第一个事件?

这将仅适用于第一个(第二个或任何@i)事件

declare @i int = 1;

update newTable
set xmlcol.modify('replace value of (/root/parent/child/field[@name="1"]/value/text())[{sql:variable("@i")}] with "newValue"');

您应该用循环中的新值替换所有值。

对于任何为我工作的人

BEGIN

declare @I int

;WITH XMLNAMESPACES(DEFAULT 'your namespace')

SELECT @I= xmlcol.value('count(/root/parent/child/field[@name="1"]/value/)', 'int')

FROM newTable

select @I as a

while @I > 0 

  begin

  ;WITH XMLNAMESPACES(DEFAULT 'your namespace')

  update newTable set xmlcol.modify('replace value of (/root/parent/child/field[@name="1"]/value/text()) with "newValue"')

  set @I = @I - 1

end
end

你试过测试吗?我之前做过,但现在无法得到结果。我正在跑步,所以不能马上测试
BEGIN

declare @I int

;WITH XMLNAMESPACES(DEFAULT 'your namespace')

SELECT @I= xmlcol.value('count(/root/parent/child/field[@name="1"]/value/)', 'int')

FROM newTable

select @I as a

while @I > 0 

  begin

  ;WITH XMLNAMESPACES(DEFAULT 'your namespace')

  update newTable set xmlcol.modify('replace value of (/root/parent/child/field[@name="1"]/value/text()) with "newValue"')

  set @I = @I - 1

end
end