XML DML for 2008 R2..How to modify nested Elements

前端 未结 1 1579
迷失自我
迷失自我 2020-12-21 20:15
      
          2 7/8\" x 1\",Drill Collar,2 3/8 PAC
          2 7/8\" x 1\",Drill Collar,2 3/8 P         


        
相关标签:
1条回答
  • 2020-12-21 20:24

    You can only insert into one place in the XML at a time so you need to do it in a loop.

    Update the nodes one at at time and exit the loop when there where no updates made.

    declare @MaxFeatures xml
    
    set @MaxFeatures = N'<MaxAllowableTorque>0</MaxAllowableTorque>
                         <MaxAllowableForce>0</MaxAllowableForce>'   
    
    declare @I int 
    set @I = 1
    
    while 1 = 1
    begin
      update Component
      set XMLDetails.modify('       
          insert sql:variable("@MaxFeatures")           
          after ((/Component/Sections/Section/Length)[sql:variable("@I")])[1]')
      where XMLDetails.exist('(/Component/Sections/Section/Length)[sql:variable("@I")]') = 1
    
      if @@rowcount = 0
        break
    
      set @I = @I + 1
    end
    
    0 讨论(0)
提交回复
热议问题