sql - 在指定位置将 xmltype 插入 xmltype [PL/SQL]

标签 sql oracle plsql oracle11g

我在将 xmltype 插入 pl/sql 指定位置的另一个 xmltype 时遇到问题。

第一个变量 v_xml 的形式为:

<ord>
  <head>
    <ord_code>123</ord_code>
    <ord_date>01-01-2015</ord_date>
  </head>
</ord>

第二个 v_xml2:

<pos>
  <pos_code>456</pos_code>
  <pos_desc>description</pos_desc>
</pos>

我的目的是得到这样的东西:

<ord>
  <head>
    <ord_code>123</ord_code>
    <ord_date>01-01-2015</ord_date>
  </head>
  <!-- put the second variable in this place - after closing <head> tag -->
  <pos>
    <pos_code>456</pos_code>
    <pos_desc>description</pos_desc>
  </pos>
</ord>

我应该如何处理我的代码?

declare
  v_xml  xmltype;
  v_xml2 xmltype;
begin
  -- some code
  -- some code
  -- v_xml and v_xml2 has the form as I define above
end;

有人能帮我解决这个问题吗?据我所知,有像 insertchildxml、appendchildxml 或类似的功能...... 我在纯 SQL 中发现的解决方案很少,但我不知道如何在 PL/SQL 中移动它。

谢谢!

最佳答案

您可以使用提到的 appendChildXML ,就像这里一样:

declare
  v_xml  xmltype := xmltype('<ord>
                               <head>
                                 <ord_code>123</ord_code>
                                 <ord_date>01-01-2015</ord_date>
                               </head>
                             </ord>');
  v_xml2 xmltype:= xmltype('<pos>
                              <pos_code>456</pos_code>
                              <pos_desc>description</pos_desc>
                            </pos>');
  v_output xmltype;
begin
  select appendChildXML(v_xml, 'ord', v_xml2) 
    into v_output from dual;

  -- output result
  dbms_output.put_line( substr( v_output.getclobval(), 1, 1000 ) );
end;

输出:

<ord>
  <head>
    <ord_code>123</ord_code>
    <ord_date>01-01-2015</ord_date>
  </head>
  <pos>
    <pos_code>456</pos_code>
    <pos_desc>description</pos_desc>
  </pos>
</ord>

关于sql - 在指定位置将 xmltype 插入 xmltype [PL/SQL],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29821779/

相关文章:

SQL返回特定行的rownum? (使用 Oracle 数据库)

java - 使用java从txt文件插入oracle DB

oracle - 如何在包中创建 PL/SQL 函数

sql - 为什么我在尝试创建这个简单函数时收到 PLS-00103?

sql - 来自 SQL 的 Web 服务

sql - 制作双插入的最佳方法

oracle - 有没有办法使用 Hibernate Criteria API 对结果进行排名?

SQL Alter 触发器挂起

SQL Server - 如何将派生表用于计算字段?

oracle - 使用 PL/SQL 从 Apex Web 服务响应中获取响应头