更新XML中的多个节点中的单个

时间:2017-12-13 20:24:18

标签: xml oracle11g toad

我需要将Russ的日期工作从21更新到22但无法找到我只能根据条件更新该节点的代码。

即使我提取,它也只给我第一个节点的数据。

SELECT EXTRACT(XMLDATA,  'root/Level1Users/employeeinfo/username')
    FROM TblUser_DATA -- gives only first username

SELECT EXTRACT(XMLDATA,  'root/Level1Users/employeeinfo/username/text()')
        FROM TblUser_DATA -- returns concatenated usernames in single row

任何指针?

<root>
  <Level1Users>
    <isTrue>false</isTrue>
    <employeeinfo>
    <username>Tissy</username>
    <role>RES</role>
     <daysworked>20</daysworked>
    <availability/>
    </employeeinfo>
    <employeeinfo>
    <username>Russ</username>
    <role>PES</role>
     <daysworked>21</daysworked>
    <availability>Yes</availability>
   </employeeinfo>
  <employeeinfo>
    <username>Amy</username>
    <role>PES</role>
     <daysworked>22</daysworked>
    <availability>Yes</availability>
   </employeeinfo>
    <by>ABC</by>
    <date>13-JUN-2017</date>
  </Level1Users>
</root>

1 个答案:

答案 0 :(得分:1)

with abc as (select xmltype('<root>
  <Level1Users>
    <isTrue>false</isTrue>
    <employeeinfo>
    <username>Tissy</username>
    <role>RES</role>
     <daysworked>20</daysworked>
    <availability/>
    </employeeinfo>
    <employeeinfo>
    <username>Russ</username>
    <role>PES</role>
     <daysworked>21</daysworked>
    <availability>Yes</availability>
   </employeeinfo>
  <employeeinfo>
    <username>Amy</username>
    <role>PES</role>
     <daysworked>22</daysworked>
    <availability>Yes</availability>
   </employeeinfo>
    <by>ABC</by>
    <date>13-JUN-2017</date>
  </Level1Users>
</root>') xml_ from dual)
select 
          xmlquery('copy $doc :=. modify
               ( for $i in $doc/root/Level1Users/employeeinfo
                 where $i/username/text() eq "Russ"
                 return replace value of node $i/daysworked with "22")
                 return $doc'
                           passing xml_ returning content)

它是如何运作的。 Xmlquery( 'xquery statement' passing {list of passed element } returning content) - &gt;函数返回xmltype

copy $doc :=. modify - 将完整$input_document复制到变量$doc。 (我们只能修改input_docuemnt的副本) (for ... return )它是&#39; flwor表达&#39; f - for,l-let,w-where,o-order。
表达意味着。对于每个employeeinfo,其中employeeinfo/username/test() = 'Russ'替换节点employeeinfo/daysworked with '22'并将新的xml文档返回到return $doc之外。

要更新,您必须在update语句中使用xmlquery。

Update table set xml_column := xmlquery('xquery' passing xml_column returning content)