如何在DB2表结构的XML中多次更新XML节点?

时间:2015-11-27 06:45:36

标签: sql db2

我们正面临DB2 SQL的问题。我们正试图检查我们是否可以找到解决方案。

问题陈述:

  1. 编写SQL以更新XML节点,该节点根据某些条件以嵌套形式多次出现。 下面是XML结构。 <RFPData>是根节点。
  2. <RFPData>
    .
    .
    .
    .
    <class dataStr="list">
                    .
    .
    .
    </class >
    <class dataStr="list">
        <classCheckedProductStr>Life &amp;lt;br/&amp;gt;Dental &amp;lt;br/&amp;gt;</classCheckedProductStr>
        <classDescriptionOptionText>All Active Full-Time Assistant Vice Presidents</classDescriptionOptionText>
        <classDescriptionOption>5</classDescriptionOption>
        <life>
            <ClsLifePayPeriod>W</ClsLifePayPeriod>
            <coverage dataStr="list">
               <status>ACTIVE</status>
                <coverageId>50</coverageId>
                <coverageName>Buy Up Life with AD&amp;D</coverageName>
                <ClassLifeBasicLifeEmployee>22</ClassLifeBasicLifeEmployee>
                <covId>50</covId>
                <ClassLifeBasicLifeDependents>34</ClassLifeBasicLifeDependents>
                <isCoverageSelected>Y</isCoverageSelected>
                <SQ>0</SQ>
            </coverage>
            <coverage dataStr="list">
                <status>ACTIVE</status>
                <coverageId>54</coverageId>
                <coverageName>Buy Up Dependent Life with AD&amp;D</coverageName>
                <ClassLifeBasicLifeEmployee>44</ClassLifeBasicLifeEmployee>
                <covId>54</covId>
                <ClassLifeBasicLifeDependents>43</ClassLifeBasicLifeDependents>
                <isCoverageSelected>Y</isCoverageSelected>
                <SQ>1</SQ>
            </coverage>
            <clsFutureEmpWaitingPeriodRule>1STFLWP</clsFutureEmpWaitingPeriodRule>
            <clsCurrentEmpWaitingPeriodRule>1STFLWP</clsCurrentEmpWaitingPeriodRule>
            <status>ACTIVE</status>
            <clsCurrentEmpAveragedOver>-1</clsCurrentEmpAveragedOver>
            <isCoverageChecked>Y</isCoverageChecked>
            <clsCurrentEmpEarningsDefinition>-1</clsCurrentEmpEarningsDefinition>
            <clsFutureEmpWaitingPeriodUnit>DY</clsFutureEmpWaitingPeriodUnit>
            <ClsCurrentEmpLifeWaitingPeriodValue>14</ClsCurrentEmpLifeWaitingPeriodValue>
            <clsCurrentEmpWaitingPeriodUnit>DY</clsCurrentEmpWaitingPeriodUnit>
            <ClsLifeHoursWorked>12</ClsLifeHoursWorked>
            <ClsFutureEmpLifeWaitingPeriodValue>33</ClsFutureEmpLifeWaitingPeriodValue>
        </life>
        <SQ>0</SQ>
    </class>
    <class dataStr="list">
                    .
    .
    .
    </class >
    .
    .
    .
    .
    </RFPData>
    
    1. 如果<classDescriptionOption>节点值为“所有有效的全职主管”,则上面蓝色突出显示的<classDescriptionOptionText>节点需要更新为值6。
    2. 但我们无法实现此目标,因为<class dataStr="list">可能存在多个<classDescriptionOption>
    3. 当我们编写SQL脚本时,它更新了我们在update语句中提供的值6的所有事件,即使我们尝试将<classDescriptionOptionText>值等同为“所有活动的全职监督员”。 / LI>

      使用的更新脚本如下。

      UPDATE T_SL_APP
      SET SL_XML_TXT = XMLQUERY('
      copy $new := $SL_XML_TXT
      modify (
      for $i in $new/RFPData/classSetup/class/classDescriptionOption  return
      do replace value of $i with "6"
      )
      return $new')
      WHERE SL_APP_ID = '1184'
      and xmlexists('$SL_XML_TXT//RFPData/classSetup/class[classDescriptionOptionText = "All Active Full-Time Supervisors"]');
      

      有人可以帮我们解决。附上XML的整个结构以供参考。

1 个答案:

答案 0 :(得分:1)

未经过测试,但尝试在XQuery中使用谓词:

copy $new := $SL_XML_TXT
modify (
     for $i in $new/RFPData/classSetup/class[classDescriptionOptionText = "All Active Full-Time Supervisors"]/classDescriptionOption   
     return
         do replace value of $i with "6"
       )
return $new

上面只会迭代那些与Supervisor一起使用OptionText的“类”节点,访问Option并更新它。