使用从XML CLOB读取的PL / SQL将行插入到oracle中

时间:2019-02-11 13:58:47

标签: xml plsql

我在使用PL / SQL时遇到了一些实际问题。

我编写了一个存储过程,该存储过程准备了XML中的记录并将其插入数据库表中。

对于单个记录输入,它将进行干净地编译。但是我正在寻找一种方法来插入一个大型XML文件的所有记录,并且无法弄清楚如何使外观正确发挥作用。

以下是适用于单个记录输入的代码:

<div class="row bg-dark">
    <div class="col-12 border">
    <canvas id="myChart"></canvas>
    </div>
</div>

<div class="container">
  <div>
    <?php
      $sth = $db->prepare("SELECT Actual FROM csvhoejde1");
      $sth->execute();
      /* Fetch all of the remaining rows in the result set */
      $result = $sth->fetchAll(PDO::FETCH_COLUMN);
      // $result = explode("@", implode(",@", $result));
      // print_r for at se resultaterne.
      echo'<pre>';
      print_r($result);
      echo'</pre>';
      $std = $db->prepare("SELECT Dato_ur_stillet FROM palle_tbs");
      $std->execute();
      /* Fetch all of the remaining rows in the result set */
      $palle = $std->fetchAll(PDO::FETCH_COLUMN);
    ?>
  <div>
</div>
  <!----------------------myChart---------------------->
  <script src="./assets/charts/dist/Chart.js"></script>
<script>



var canvas = document.getElementById("myChart");
var ctx = canvas.getContext("2d");

var horizonalLinePlugin = {
  afterDraw: function(chartInstance) {
    var yScale = chartInstance.scales["y-axis-0"];
    var canvas = chartInstance.chart;
    var ctx = canvas.ctx;
    var index;
    var line;
    var style;

    if (chartInstance.options.horizontalLine) {
      for (index = 0; index < chartInstance.options.horizontalLine.length; index++) {
        line = chartInstance.options.horizontalLine[index];

        if (!line.style) {
          style = "rgba(169,169,169, .6)";
        } else {
          style = line.style;
        }

        if (line.y) {
          yValue = yScale.getPixelForValue(line.y);
        } else {
          yValue = 0;
        }

        ctx.lineWidth = 3;

        if (yValue) {
          ctx.beginPath();
          ctx.moveTo(0, yValue);
          ctx.lineTo(canvas.width, yValue);
          ctx.strokeStyle = style;
          ctx.stroke();
        }

        if (line.text) {
          ctx.fillStyle = style;
          ctx.fillText(line.text, 0, yValue + ctx.lineWidth);
        }
      }
      return;
    };
  }
};
Chart.pluginService.register(horizonalLinePlugin);

var data = {
  labels: [<?php echo join($palle, ',') ?>],
  datasets: [{
    label: "My First dataset",
    fill: false,
    lineTension: 0.1,
    backgroundColor: "rgba(75,192,192,0.4)",
    borderColor: "rgba(75,192,192,1)",
    borderCapStyle: 'butt',
    borderDash: [],
    borderDashOffset: 0.0,
    borderJoinStyle: 'miter',
    pointBorderColor: "rgba(75,192,192,1)",
    pointBackgroundColor: "#fff",
    pointBorderWidth: 1,
    pointHoverRadius: 5,
    pointHoverBackgroundColor: "rgba(75,192,192,1)",
    pointHoverBorderColor: "rgba(220,220,220,1)",
    pointHoverBorderWidth: 2,
    pointRadius: 4,
    pointHitRadius: 10,
    data: [<?php echo join($result, ',') ?>],
  }]
};

var myChart = new Chart(ctx, {
  type: 'line',
  data: data,
  options: {
    "horizontalLine": [{
      "y": 140,
      "style": "rgba(255, 0, 0, .4)",
    }, {
      "y": 120,
      "style": "#00ffff",
    }]
  }
});
</script>

您可以看到我注释掉了我失败的循环尝试。有人可以在这里帮助我,为什么我在XML中循环然后插入不起作用?

谢谢

汤姆


更新-这是XML示例

    CREATE OR REPLACE EDITIONABLE PROCEDURE "DORTO"."DORTOSALES" (P_DE_SALES CLOB)
AS
I_ORTP VARCHAR2 (6);
I_ORNO VARCHAR2 (20);
I_IVNO NUMBER (9);
I_CUNO VARCHAR2 (20);
I_YREF VARCHAR2 (60);
I_OREF VARCHAR2 (60);
I_CUOR VARCHAR2 (40);
I_ITCL VARCHAR2 (10);
I_ITNO VARCHAR2 (30);
I_IVQT NUMBER (15);
I_BANO VARCHAR2 (40);
I_IVDT NUMBER (8);
I_DLIX NUMBER (11);
I_ELNO VARCHAR2 (16);
I_PONR NUMBER (5);
I_CNNO VARCHAR2 (20);
I_CNIV NUMBER (9);
I_TRANSFER TIMESTAMP(6);
BEGIN

  -- FOR i IN (

   SELECT EXTRACTVALUE(xmltype(P_DE_SALES), '/dortoSalesInputIF/order/orderType')
  ,EXTRACTVALUE(xmltype(P_DE_SALES), '/dortoSalesInputIF/order/orderNumber')
  ,EXTRACTVALUE(xmltype(P_DE_SALES), '/dortoSalesInputIF/order/customerNumber')
  ,EXTRACTVALUE(xmltype(P_DE_SALES), '/dortoSalesInputIF/order/customerOrderReference')
  ,EXTRACTVALUE(xmltype(P_DE_SALES), '/dortoSalesInputIF/order/batchNumber')
  ,EXTRACTVALUE(xmltype(P_DE_SALES), '/dortoSalesInputIF/order/deliveryNoteNumber')
  ,EXTRACTVALUE(xmltype(P_DE_SALES), '/dortoSalesInputIF/order/elementNumber')
  ,EXTRACTVALUE(xmltype(P_DE_SALES), '/dortoSalesInputIF/order/orderLineNumber')
  ,EXTRACTVALUE(xmltype(P_DE_SALES), '/dortoSalesInputIF/creditNote/creditNoteOrderNumber')
  ,EXTRACTVALUE(xmltype(P_DE_SALES), '/dortoSalesInputIF/creditNote/orderNumberCreditNote')
  ,EXTRACTVALUE(xmltype(P_DE_SALES), '/dortoSalesInputIF/creditNote/creditNoteNumber')
  ,EXTRACTVALUE(xmltype(P_DE_SALES), '/dortoSalesInputIF/creditNote/creditNoteInvoiceNumber')
  ,EXTRACTVALUE(xmltype(P_DE_SALES), '/dortoSalesInputIF/invoice/invoiceNumber')
  ,EXTRACTVALUE(xmltype(P_DE_SALES), '/dortoSalesInputIF/invoice/itemClassification')
  ,EXTRACTVALUE(xmltype(P_DE_SALES), '/dortoSalesInputIF/invoice/itemMaterialNumber')
  ,EXTRACTVALUE(xmltype(P_DE_SALES), '/dortoSalesInputIF/invoice/invoicedQuantity')
  ,EXTRACTVALUE(xmltype(P_DE_SALES), '/dortoSalesInputIF/invoice/invoiceDate')
 INTO I_ORTP,I_ORNO,I_CUNO,I_CUOR,I_BANO,I_DLIX,I_ELNO,I_PONR,I_YREF,I_OREF,I_CNNO,I_CNIV,I_IVNO,I_ITCL,I_ITNO,I_IVQT,I_IVDT
 FROM TABLE(XMLSequence(xmltype(P_DE_SALES))) x ; --LOOP
 INSERT INTO DE_SALES (ORTP,ORNO,CUNO,CUOR,BANO,DLIX,ELNO,PONR,YREF,OREF,CNNO,CNIV,IVNO,ITCL,ITNO,IVQT,IVDT,TRANSFER) VALUES
 --(i.I_ORTP,i.I_ORNO,i.I_CUNO,i.I_CUOR,i.I_BANO,i.I_DLIX,i.I_ELNO,i.I_PONR,i.I_YREF,i.I_OREF,i.I_CNNO,i.I_CNIV,i.I_IVNO,i.I_ITCL,i.I_ITNO,i.I_IVQT,i.I_IVDT,sysdate);

 (I_ORTP,I_ORNO,I_CUNO,I_CUOR,I_BANO,I_DLIX,I_ELNO,I_PONR,I_YREF,I_OREF,I_CNNO,I_CNIV,I_IVNO,I_ITCL,I_ITNO,I_IVQT,I_IVDT,sysdate);

 --END LOOP;

 COMMIT;
END;

更新:

我尝试了以下操作-大大简化了XML结构-包括要插入数据库中的两个记录。

单个插入(仅在XML中有记录)可以正常工作,但是只有两个插入失败的插入。

我收到以下错误:

ORA-19279:XPTY0004-XQuery动态类型不匹配:预期的单例序列-获得了多个项目序列

ORA-06512:在第67行

ORA-06512:在第67行

ORA-06512:第121行

<?xml version="1.0" encoding="ISO-8859-1"?>
<dortoSalesInputIF>
    <order>
        <orderType>DO1</orderType>
        <orderNumber>1304315</orderNumber>
        <customerNumber>5104081</customerNumber>
        <customerOrderReference></customerOrderReference>
        <batchNumber></batchNumber>
        <deliveryNoteNumber>8337697</deliveryNoteNumber>
        <elementNumber></elementNumber>
        <orderLineNumber>2</orderLineNumber>
    </order>
    <creditNote>
        <creditNoteOrderNumber>617</creditNoteOrderNumber>
        <orderNumberCreditNote>59237</orderNumberCreditNote>
        <creditNoteNumber></creditNoteNumber>
        <creditNoteInvoiceNumber></creditNoteInvoiceNumber>
    </creditNote>
    <invoice>
        <invoiceNumber>31615915</invoiceNumber>
        <itemClassification>TRA</itemClassification>
        <itemMaterialNumber>100000101</itemMaterialNumber>
        <invoicedQuantity>1</invoicedQuantity>
        <invoiceDate>20160531</invoiceDate>
    </invoice>
</dortoSalesInputIF>
<dortoSalesInputIF>
    <order>
        <orderType>DO1</orderType>
        <orderNumber>1304316</orderNumber>
        <customerNumber>5104082</customerNumber>
        <customerOrderReference></customerOrderReference>
        <batchNumber></batchNumber>
        <deliveryNoteNumber>8337698</deliveryNoteNumber>
        <elementNumber></elementNumber>
        <orderLineNumber>2</orderLineNumber>
    </order>
    <creditNote>
        <creditNoteOrderNumber>618</creditNoteOrderNumber>
        <orderNumberCreditNote>59238</orderNumberCreditNote>
        <creditNoteNumber></creditNoteNumber>
        <creditNoteInvoiceNumber></creditNoteInvoiceNumber>
    </creditNote>
    <invoice>
        <invoiceNumber>31615916</invoiceNumber>
        <itemClassification>TRA</itemClassification>
        <itemMaterialNumber>100000101</itemMaterialNumber>
        <invoicedQuantity>1</invoicedQuantity>
        <invoiceDate>20160531</invoiceDate>
    </invoice>
</dortoSalesInputIF>

最终更新

这是可行的

declare
  l_xml      xmltype := xmltype('<?xml version="1.0" encoding="ISO-8859-1"?>
<dortoSalesInputIF>
    <order>
        <orderType>DO1</orderType>
        <orderNumber>1304315</orderNumber>
        <customerNumber>5104081</customerNumber>
        <customerOrderReference></customerOrderReference>
        <batchNumber></batchNumber>
        <deliveryNoteNumber>8337697</deliveryNoteNumber>
        <elementNumber></elementNumber>
        <orderLineNumber>2</orderLineNumber>
        <creditNoteOrderNumber>617</creditNoteOrderNumber>
        <orderNumberCreditNote>59237</orderNumberCreditNote>
        <creditNoteNumber></creditNoteNumber>
        <creditNoteInvoiceNumber></creditNoteInvoiceNumber>
        <invoiceNumber>31615915</invoiceNumber>
        <itemClassification>TRA</itemClassification>
        <itemMaterialNumber>100000101</itemMaterialNumber>
        <invoicedQuantity>1</invoicedQuantity>
        <invoiceDate>20160531</invoiceDate>
    </order>
    <order>
        <orderType>DO1</orderType>
        <orderNumber>1304316</orderNumber>
        <customerNumber>5104082</customerNumber>
        <customerOrderReference></customerOrderReference>
        <batchNumber></batchNumber>
        <deliveryNoteNumber>8337698</deliveryNoteNumber>
        <elementNumber></elementNumber>
        <orderLineNumber>2</orderLineNumber>
        <creditNoteOrderNumber>618</creditNoteOrderNumber>
        <orderNumberCreditNote>59238</orderNumberCreditNote>
        <creditNoteNumber></creditNoteNumber>
        <creditNoteInvoiceNumber></creditNoteInvoiceNumber>
        <invoiceNumber>31615916</invoiceNumber>
        <itemClassification>TRA</itemClassification>
        <itemMaterialNumber>100000101</itemMaterialNumber>
        <invoicedQuantity>1</invoicedQuantity>
        <invoiceDate>20160531</invoiceDate>
    </order>
</dortoSalesInputIF>');

procedure DORTOSALES (i_clob in xmltype)
AS
  I_ORTP     varchar2(6);
  I_ORNO     varchar2(20);
  I_IVNO     number(9);
  I_CUNO     varchar2(20);
  I_YREF     varchar2(60);
  I_OREF     varchar2(60);
  I_CUOR     varchar2(40);
  I_ITCL     varchar2(10);
  I_ITNO     varchar2(30);
  I_IVQT     number(15);
  I_BANO     varchar2(40);
  I_IVDT     number(8);
  I_DLIX     number(11);
  I_ELNO     varchar2(16);
  I_PONR     number(5);
  I_CNNO     varchar2(20);
  I_CNIV     number(9);
  I_TRANSFER timestamp(6);

begin

  for i in  
  (
    SELECT  xt.*
    FROM    XMLTABLE ('/dortoSalesInputIF'
            PASSING  l_xml
            COLUMNS
              orderType               PATH '/dortoSalesInputIF/order/orderType',
              orderNumber             PATH '/dortoSalesInputIF/order/orderNumber',
              customerNumber          PATH '/dortoSalesInputIF/order/customerNumber',
              customerOrderReference  PATH '/dortoSalesInputIF/order/customerOrderReference',
              batchNumber             PATH '/dortoSalesInputIF/order/batchNumber',
              deliveryNoteNumber      PATH '/dortoSalesInputIF/order/deliveryNoteNumber',
              elementNumber           PATH '/dortoSalesInputIF/order/elementNumber',
              orderLineNumber         PATH '/dortoSalesInputIF/order/orderLineNumber',
              creditNoteOrderNumber   PATH '/dortoSalesInputIF/order/creditNoteOrderNumber',
              orderNumberCreditNote   PATH '/dortoSalesInputIF/order/orderNumberCreditNote',
              creditNoteNumber        PATH '/dortoSalesInputIF/order/creditNoteNumber',
              creditNoteInvoiceNumber PATH '/dortoSalesInputIF/order/creditNoteInvoiceNumber',
              invoiceNumber           PATH '/dortoSalesInputIF/order/invoiceNumber',
              itemClassification      PATH '/dortoSalesInputIF/order/itemClassification',
              itemMaterialNumber      PATH '/dortoSalesInputIF/order/itemMaterialNumber',
              invoicedQuantity        PATH '/dortoSalesInputIF/order/invoicedQuantity',
              invoiceDate             PATH '/dortoSalesInputIF/order/invoiceDate'
            ) xt
  )
  loop
    INSERT INTO DE_SALES (ORTP,ORNO,CUNO,CUOR,BANO,DLIX,ELNO,PONR,YREF,OREF,CNNO,CNIV,IVNO,ITCL,ITNO,IVQT,IVDT,TRANSFER) VALUES
   (i.orderType             ,
    i.orderNumber           ,
    i.customerNumber        ,
    i.customerOrderReference,
    i.batchNumber           ,
    i.deliveryNoteNumber    ,
    i.elementNumber         ,
    i.orderLineNumber       ,
    i.creditNoteOrderNumber ,
    i.orderNumberCreditNote ,
    i.creditNoteNumber      ,
    i.creditNoteInvoiceNumber,
    i.invoiceNumber         ,
    i.itemClassification    ,
    i.itemMaterialNumber    ,
    i.invoicedQuantity      ,
    i.invoiceDate           ,
    sysdate);


  END LOOP;

  COMMIT;
END;


begin
  DORTOSALES(l_xml);
end;

2 个答案:

答案 0 :(得分:0)

这适用于单个dortoSalesInputIF节点:

declare
  l_xml      xmltype := xmltype('<?xml version="1.0" encoding="ISO-8859-1"?>
<dortoSalesInputIF>
    <order>
        <orderType>DO1</orderType>
        <orderNumber>1304315</orderNumber>
        <customerNumber>5104081</customerNumber>
        <customerOrderReference></customerOrderReference>
        <batchNumber></batchNumber>
        <deliveryNoteNumber>8337697</deliveryNoteNumber>
        <elementNumber></elementNumber>
        <orderLineNumber>2</orderLineNumber>
    </order>
    <creditNote>
        <creditNoteOrderNumber>617</creditNoteOrderNumber>
        <orderNumberCreditNote>59237</orderNumberCreditNote>
        <creditNoteNumber></creditNoteNumber>
        <creditNoteInvoiceNumber></creditNoteInvoiceNumber>
    </creditNote>
    <invoice>
        <invoiceNumber>31615915</invoiceNumber>
        <itemClassification>TRA</itemClassification>
        <itemMaterialNumber>100000101</itemMaterialNumber>
        <invoicedQuantity>1</invoicedQuantity>
        <invoiceDate>20160531</invoiceDate>
    </invoice>
</dortoSalesInputIF>'); 

procedure DORTOSALES (i_clob in xmltype)
AS
  I_ORTP     varchar2(6);
  I_ORNO     varchar2(20);
  I_IVNO     number(9);
  I_CUNO     varchar2(20);
  I_YREF     varchar2(60);
  I_OREF     varchar2(60);
  I_CUOR     varchar2(40);
  I_ITCL     varchar2(10);
  I_ITNO     varchar2(30);
  I_IVQT     number(15);
  I_BANO     varchar2(40);
  I_IVDT     number(8);
  I_DLIX     number(11);
  I_ELNO     varchar2(16);
  I_PONR     number(5);
  I_CNNO     varchar2(20);
  I_CNIV     number(9);
  I_TRANSFER timestamp(6);
begin

  for i in  
  (
    SELECT  xt.*
    FROM    XMLTABLE ('/dortoSalesInputIF'
            PASSING  l_xml
            COLUMNS
              orderType               PATH '/dortoSalesInputIF/order/orderType',
              orderNumber             PATH '/dortoSalesInputIF/order/orderNumber',
              customerNumber          PATH '/dortoSalesInputIF/order/customerNumber',
              customerOrderReference  PATH '/dortoSalesInputIF/order/customerOrderReference',
              batchNumber             PATH '/dortoSalesInputIF/order/batchNumber',
              deliveryNoteNumber      PATH '/dortoSalesInputIF/order/deliveryNoteNumber',
              elementNumber           PATH '/dortoSalesInputIF/order/elementNumber',
              orderLineNumber         PATH '/dortoSalesInputIF/order/orderLineNumber',
              creditNoteOrderNumber   PATH '/dortoSalesInputIF/creditNote/creditNoteOrderNumber',
              orderNumberCreditNote   PATH '/dortoSalesInputIF/creditNote/orderNumberCreditNote',
              creditNoteNumber        PATH '/dortoSalesInputIF/creditNote/creditNoteNumber',
              creditNoteInvoiceNumber PATH '/dortoSalesInputIF/creditNote/creditNoteInvoiceNumber',
              invoiceNumber           PATH '/dortoSalesInputIF/invoice/invoiceNumber',
              itemClassification      PATH '/dortoSalesInputIF/invoice/itemClassification',
              itemMaterialNumber      PATH '/dortoSalesInputIF/invoice/itemMaterialNumber',
              invoicedQuantity        PATH '/dortoSalesInputIF/invoice/invoicedQuantity',
              invoiceDate             PATH '/dortoSalesInputIF/invoice/invoiceDate'
            ) xt
  )
  loop
    INSERT INTO DE_SALES (ORTP,ORNO,CUNO,CUOR,BANO,DLIX,ELNO,PONR,YREF,OREF,CNNO,CNIV,IVNO,ITCL,ITNO,IVQT,IVDT,TRANSFER) VALUES
   (i.orderType             ,
    i.orderNumber           ,
    i.customerNumber        ,
    i.customerOrderReference,
    i.batchNumber           ,
    i.deliveryNoteNumber    ,
    i.elementNumber         ,
    i.orderLineNumber       ,
    i.creditNoteOrderNumber ,
    i.orderNumberCreditNote ,
    i.creditNoteNumber      ,
    i.creditNoteInvoiceNumber,
    i.invoiceNumber         ,
    i.itemClassification    ,
    i.itemMaterialNumber    ,
    i.invoicedQuantity      ,
    i.invoiceDate           ,
    sysdate);


  END LOOP;

  COMMIT;
END;

begin
  DORTOSALES(l_xml);
end;
/

但是:

如果您将xml放在文件之间:

<t>
<dortoSalesInputIF>
..
</dortoSalesInputIF>
<dortoSalesInputIF>
..
</dortoSalesInputIF>
<dortoSalesInputIF>
..
</dortoSalesInputIF>
</t>

此代码也将起作用

答案 1 :(得分:0)

下面是一个示例,您应该可以对其进行更新以添加到其余列中:

WITH your_xml AS (SELECT 1 id, XMLTYPE('<toplevel><dortoSalesInputIF>
    <order>
        <orderType>DO1</orderType>
        <orderNumber>1304315</orderNumber>
        <customerNumber>5104081</customerNumber>
        <customerOrderReference></customerOrderReference>
        <batchNumber></batchNumber>
        <deliveryNoteNumber>8337697</deliveryNoteNumber>
        <elementNumber></elementNumber>
        <orderLineNumber>2</orderLineNumber>
    </order>
    <creditNote>
        <creditNoteOrderNumber>617</creditNoteOrderNumber>
        <orderNumberCreditNote>59237</orderNumberCreditNote>
        <creditNoteNumber></creditNoteNumber>
        <creditNoteInvoiceNumber></creditNoteInvoiceNumber>
    </creditNote>
    <invoice>
        <invoiceNumber>31615915</invoiceNumber>
        <itemClassification>TRA</itemClassification>
        <itemMaterialNumber>100000101</itemMaterialNumber>
        <invoicedQuantity>1</invoicedQuantity>
        <invoiceDate>20160531</invoiceDate>
    </invoice>
</dortoSalesInputIF>
<dortoSalesInputIF>
    <order>
        <orderType>DO1</orderType>
        <orderNumber>1304316</orderNumber>
        <customerNumber>5104082</customerNumber>
        <customerOrderReference></customerOrderReference>
        <batchNumber></batchNumber>
        <deliveryNoteNumber>8337698</deliveryNoteNumber>
        <elementNumber></elementNumber>
        <orderLineNumber>2</orderLineNumber>
    </order>
    <creditNote>
        <creditNoteOrderNumber>618</creditNoteOrderNumber>
        <orderNumberCreditNote>59238</orderNumberCreditNote>
        <creditNoteNumber></creditNoteNumber>
        <creditNoteInvoiceNumber></creditNoteInvoiceNumber>
    </creditNote>
    <invoice>
        <invoiceNumber>31615916</invoiceNumber>
        <itemClassification>TRA</itemClassification>
        <itemMaterialNumber>100000101</itemMaterialNumber>
        <invoicedQuantity>1</invoicedQuantity>
        <invoiceDate>20160531</invoiceDate>
    </invoice>
</dortoSalesInputIF>
</toplevel>') xml_data FROM dual UNION ALL
              SELECT 2 id, XMLTYPE('<toplevel><dortoSalesInputIF>
    <order>
        <orderType>DO2</orderType>
        <orderNumber>1</orderNumber>
        <customerNumber>2</customerNumber>
        <customerOrderReference></customerOrderReference>
        <batchNumber></batchNumber>
        <deliveryNoteNumber>3</deliveryNoteNumber>
        <elementNumber></elementNumber>
        <orderLineNumber>42</orderLineNumber>
    </order>
    <creditNote>
        <creditNoteOrderNumber>5</creditNoteOrderNumber>
        <orderNumberCreditNote>6</orderNumberCreditNote>
        <creditNoteNumber></creditNoteNumber>
        <creditNoteInvoiceNumber></creditNoteInvoiceNumber>
    </creditNote>
    <invoice>
        <invoiceNumber>7</invoiceNumber>
        <itemClassification>A</itemClassification>
        <itemMaterialNumber>8</itemMaterialNumber>
        <invoicedQuantity>9</invoicedQuantity>
        <invoiceDate>10</invoiceDate>
    </invoice>
</dortoSalesInputIF>
<dortoSalesInputIF>
    <order>
        <orderType>DO1</orderType>
        <orderNumber>11</orderNumber>
        <customerNumber>12</customerNumber>
        <customerOrderReference></customerOrderReference>
        <batchNumber></batchNumber>
        <deliveryNoteNumber>13</deliveryNoteNumber>
        <elementNumber></elementNumber>
        <orderLineNumber>14</orderLineNumber>
    </order>
    <creditNote>
        <creditNoteOrderNumber>15</creditNoteOrderNumber>
        <orderNumberCreditNote>16</orderNumberCreditNote>
        <creditNoteNumber></creditNoteNumber>
        <creditNoteInvoiceNumber></creditNoteInvoiceNumber>
    </creditNote>
    <invoice>
        <invoiceNumber>17</invoiceNumber>
        <itemClassification>TRA</itemClassification>
        <itemMaterialNumber>18</itemMaterialNumber>
        <invoicedQuantity>19</invoicedQuantity>
        <invoiceDate>20</invoiceDate>
    </invoice>
</dortoSalesInputIF>
</toplevel>') xml_data FROM dual)
SELECT *
FROM   your_xml yx
       CROSS JOIN XMLTABLE('/toplevel/dortoSalesInputIF' PASSING yx.xml_data
                           COLUMNS order_type VARCHAR2(5) PATH 'order/orderType',
                                   order_number NUMBER PATH 'order/orderNumber');

这适用于XML文档,其中包含一个“ dortoSalesInputIF”节点以及多个(如我所示)的节点。我还一次性传递了两个XML文档,假定您的XML存储在一个表中(如果是这样,您将用表名替换对“ your_xml”的引用)。

否则,如果将XML作为参数传递,则可以直接在XMLTABLE中直接传递参数-即,您不需要加入,只需执行以下操作:

select * from xmltable(... passing your_xml_parameter columns ...)

一旦有了select语句,就可以在插入语句中使用它,例如:

insert into your_table (col1, col2, ....)
select col1, col2, ...
from   .....

这将比您的过程“获取一行,将数据放入变量,将一行插入表中”要快,因为数据库完成了所有繁重的工作,并且没有在SQL和PL / SQL之间进行上下文切换,并且没有传递数据。

将插入添加到PL / SQL很容易;您只需在开始/结束内对其进行定位,如下所示:

create or replace procedure your_procedure (in_xml xmltype)
is
begin
  insert into your_table (col1, col2, ....)
  select col1, col2, ...
  from   xmltable(... passing in_xml
                  columns ...);
end;
/

(如果这是包中的过程,则不需要create or replace/。)