【问题标题】:Update Single out of multiple nodes in XML更新 XML 中多个节点中的单个节点
【发布时间】:2018-05-27 20:18:48
【问题描述】:

我需要将 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>

【问题讨论】:

    标签: xml oracle11g toad


    【解决方案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) -> 函数返回xmltype

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多