PL / SQL创建XML的过程

时间:2018-02-14 16:40:22

标签: sql oracle plsql

我对PL / SQL非常缺乏经验,我的任务是创建一个使用需要两个参数的程序,并为我当前的select语句创建XML。我有点点零碎,但我很难找到如何将它们放在一起。

所以我知道我需要从以下开始:

PROCEDURE markviewimport_interface c_markviewimport.invoice_id NUMBER(10) = NULL c_markviewimport.filename nvarchar(30) = NULL

我知道我需要一个光标才能逐行收集数据。

CURSOR c_markviewimport IS SELECT DISTINCT inv.invoice_id, 
                        vendor.segment1 vendor_num, 
                        vendor.vendor_name, 
                        poh.segment1    PO_NUMBER, 
                        inv.invoice_date, 
                        inv.invoice_num, 
                        terms.name      TERMS_NAME, 
                        inv.invoice_amount, 
                        inv.amount_applicable_to_discount, 
                        inv.amount_paid, 
                        pmt.check_date  PAYMENT_DATE, 
                        path.filename, 
                        path.complete_filename, 
                        path.document_id, 
                        stamps.text, 
                        stamps.tool_name 
        FROM   apps.ap_invoices_all inv, 
               apps.ap_invoice_distributions_all dist, 
               apps.po_distributions_all podi, 
               apps.ap_invoice_payment_history_v pmt, 
               apps.fnd_attached_docs_form_vl fnd, 
               markview.mv_page_image_paths path, 
               apps.po_vendors vendor, 
               apps.po_headers_all poh, 
               apps.ap_terms terms, 
               (SELECT mp.document_id, 
                       moi.markup_object_id, 
                       moi.page_markups_view_id, 
                       moi.text, 
                       mvt.tool_name, 
                       mp.page_id 
                FROM   markview.mv_markup_object moi, 
                       markview.mv_tool mvt, 
                       markview.mv_page_markups_view mpmv, 
                       markview.mv_page mp 
                WHERE  moi.tool_id = mvt.tool_id 
                       AND mp.page_id = mpmv.page_id 
                       AND mpmv.page_markups_view_id = moi.page_markups_view_id 
                       AND mvt.tool_id IN (SELECT mvt.tool_id 
                                           FROM   markview.mv_tool 
                                           WHERE  mvt.tool_name IN ( 
                           'Green Text', 'Blue Sticky Note' ) 
                                          )) stamps 
        WHERE  inv.invoice_id = To_number(fnd.pk1_value) 
               AND inv.invoice_id = dist.invoice_id 
               AND poh.po_header_id(+) = podi.po_header_id 
               AND podi.po_distribution_id(+) = dist.po_distribution_id 
               AND fnd.file_name = To_char(path.document_id) 
               AND inv.invoice_id = pmt.invoice_id 
               AND path.document_id = stamps.document_id(+) 
               AND path.page_id = stamps.page_id(+) 
               AND fnd.category_description = 'MarkView Document' 
               AND fnd.entity_name = 'AP_INVOICES' 
               AND INV.vendor_id = poh.vendor_id(+) 
               AND INV.terms_id = TERMS.term_id 
               AND inv.vendor_id = vendor.vendor_id 
               AND path.platform_name = 'UNIX_FS_TO_DOC_SERVER' 
               AND pmt.void = 'N') r_markviewimport 

最后,这就是我用于包含我需要的每一列的XML的内容。

SELECT XMLELEMENT("Item", Xmlforest(r_markviewimport.invoice_id, 
       r_markviewimport.vendor_num, r_markviewimport.vendor_name, 
       r_markviewimport.po_number, 
       r_markviewimport.invoice_date, r_markviewimport.invoice_num, 
       r_markviewimport.terms_name, r_markviewimport.invoice_amount, 
       r_markviewimport.amount_applicable_to_discount, 
       r_markviewimport.amount_paid, 
       r_markviewimport.payment_date, 
       r_markviewimport.filename, r_markviewimport.complete_filename, 
       r_markviewimport.document_id, r_markviewimport.text, 
       r_markviewimport.tool_name 
       )) "Item Element" 

我想我只需要帮助,或指出正确的方向来创建这个程序并将它们放在一起。

1 个答案:

答案 0 :(得分:0)

There are several ways从Oracle中的表生成XML。

使用SQL函数(如XMLELEMENT和XMLFOREST)的主要好处是必须逐行遍历游标 - 您可以完成所有工作在一个查询中。相反,如果你想用这样的游标逐行进行,使用DBMS_XMLGEN可能会更有意义。如果在将数据写入XML之前对数据进行了大量更改,则可能需要这样做。

这是使用XML SQL函数执行此操作的过程。

create or replace procedure markviewimport_interface (p_invoice_id number(10), p_filename nvarchar(30))
is
    v_output clob;
begin

    SELECT XMLELEMENT("Items", XMLAGG(XMLELEMENT("Item", Xmlforest(r_markviewimport.invoice_id, 
       r_markviewimport.vendor_num, r_markviewimport.vendor_name, 
       r_markviewimport.po_number, 
       r_markviewimport.invoice_date, r_markviewimport.invoice_num, 
       r_markviewimport.terms_name, r_markviewimport.invoice_amount, 
       r_markviewimport.amount_applicable_to_discount, 
       r_markviewimport.amount_paid, 
       r_markviewimport.payment_date, 
       r_markviewimport.filename, r_markviewimport.complete_filename, 
       r_markviewimport.document_id, r_markviewimport.text, 
       r_markviewimport.tool_name 
       )))).getclobval() into v_output
    FROM
    (SELECT DISTINCT inv.invoice_id, 
                        vendor.segment1 vendor_num, 
                        vendor.vendor_name, 
                        poh.segment1    PO_NUMBER, 
                        inv.invoice_date, 
                        inv.invoice_num, 
                        terms.name      TERMS_NAME, 
                        inv.invoice_amount, 
                        inv.amount_applicable_to_discount, 
                        inv.amount_paid, 
                        pmt.check_date  PAYMENT_DATE, 
                        path.filename, 
                        path.complete_filename, 
                        path.document_id, 
                        stamps.text, 
                        stamps.tool_name 
        FROM   apps.ap_invoices_all inv, 
               apps.ap_invoice_distributions_all dist, 
               apps.po_distributions_all podi, 
               apps.ap_invoice_payment_history_v pmt, 
               apps.fnd_attached_docs_form_vl fnd, 
               markview.mv_page_image_paths path, 
               apps.po_vendors vendor, 
               apps.po_headers_all poh, 
               apps.ap_terms terms, 
               (SELECT mp.document_id, 
                       moi.markup_object_id, 
                       moi.page_markups_view_id, 
                       moi.text, 
                       mvt.tool_name, 
                       mp.page_id 
                FROM   markview.mv_markup_object moi, 
                       markview.mv_tool mvt, 
                       markview.mv_page_markups_view mpmv, 
                       markview.mv_page mp 
                WHERE  moi.tool_id = mvt.tool_id 
                       AND mp.page_id = mpmv.page_id 
                       AND mpmv.page_markups_view_id = moi.page_markups_view_id 
                       AND mvt.tool_id IN (SELECT mvt.tool_id 
                                           FROM   markview.mv_tool 
                                           WHERE  mvt.tool_name IN ( 
                           'Green Text', 'Blue Sticky Note' ) 
                                          )) stamps 
        WHERE  inv.invoice_id = To_number(fnd.pk1_value) 
               AND inv.invoice_id = dist.invoice_id 
               AND poh.po_header_id(+) = podi.po_header_id 
               AND podi.po_distribution_id(+) = dist.po_distribution_id 
               AND fnd.file_name = To_char(path.document_id) 
               AND inv.invoice_id = pmt.invoice_id 
               AND path.document_id = stamps.document_id(+) 
               AND path.page_id = stamps.page_id(+) 
               AND fnd.category_description = 'MarkView Document' 
               AND fnd.entity_name = 'AP_INVOICES' 
               AND INV.vendor_id = poh.vendor_id(+) 
               AND INV.terms_id = TERMS.term_id 
               AND inv.vendor_id = vendor.vendor_id 
               AND path.platform_name = 'UNIX_FS_TO_DOC_SERVER' 
               AND pmt.void = 'N') r_markviewimport;

    DBMS_XSLPROCESSOR.clob2file(v_output, 'MY_DIRECTORY', 'output.xml');
    --return v_output;

end markviewimport_interface;
/