xml-sql: update multiple nodes

前端 未结 1 1064
谎友^
谎友^ 2020-12-10 05:12

I have the following problem. i have a xml file stored in a sql database. i should change all the VALUE tag values by dividing per 100. here is an extract the structure of t

相关标签:
1条回答
  • 2020-12-10 05:54

    replace value of can only update one node at a time.

    Find the max number of nodes used in all the XML's you want to update and use the loop variable in the update statement to modify one node at a time.

    The where clause checks for the existence if nodes to modify. Without that you would modify every row in the table for each iteration.

    declare @I int
    
    select @I = max(xml_badm.value('count(/ROOT/HEIGHTC/VALUE)', 'int'))
    from YourTable
    
    while @I > 0 
    begin
      update YourTable
      set xml_badm.modify
        ('replace value of ((/ROOT/HEIGHTC/VALUE)[sql:variable("@I")]/text())[1]
          with ((/ROOT/HEIGHTC/VALUE)[sql:variable("@I")]/text())[1] * 0.01')
      where xml_badm.exist('(/ROOT/HEIGHTC/VALUE)[sql:variable("@I")]') = 1
      set @I = @I - 1
    end
    
    0 讨论(0)
提交回复
热议问题