Oracle XMLQuery传递参数值

时间:2016-02-26 08:37:21

标签: sql xml oracle

在我的XMLQuery中需要帮助。

获取循环seq

SELECT extractvalue(update_xmldoc, 'count(/invoice/AR_ITEMS/ITEMS[NAME="Voice" and ITEM_TOTAL!=0])')
    INTO item_check
    FROM xml_billrun_files
    WHERE seq_id = seq
    AND docname  = REPLACE(fn,'.','_HULK.')
  ORDER BY TIMESTAMP DESC;

然后item_check值将传递给循环

FOR lp IN 1..item_check LOOP
      SELECT tst, bill_info_id, bill_no, dur_net_amt, dur_vat_amt, dur_pro_net_amt, dur_pro_vat_amt, sys_descr
      INTO xmlU, bp, bill, net_amt1, vat_amt1, net_amt2, vat_amt2, disc_name
      FROM
        (SELECT row_number() over (partition BY t.docname order by t.timestamp DESC) rn,
          t.docname,
          XMLQuery('for $i in distinct-values(/invoice/AR_ITEMS['||lp||']/ITEMS/EVENTS/BAL_IMPACTS/DISCOUNT_INFO)
                    where $i = "Plan499 Corp Disc" 
                    or $i = "Plan899 Corp Disc" 
                    or $i = "Plan1099 Corp Disc" 
                    or $i = "Plan1599 Corp Disc"
                    return $i' passing original_xmldoc returning content ).getStringVal() sys_descr

我只发布了我脚本的一部分。

脚本很好,但是当我在XMLQuery中传递lp的值时,我得到了错误

  

错误(528,66):PL / SQL:ORA-19109:RETURNING关键字预期

好的,以下是我的脚本示例。

DECLARE
item_check NUMBER;
fn VARCHAR2(100) := 'test.xml';
seq NUMBER := 1;
BEGIN

SELECT extractvalue(update_xmldoc, 'count(/invoice/AR_ITEMS/ITEMS[NAME="Voice" and ITEM_TOTAL!=0])')
    INTO item_check
    FROM xml_billrun_files
    WHERE seq_id = seq
    AND docname  = REPLACE(fn,'.','_HULK.')
    ORDER BY TIMESTAMP DESC;

    IF item_check <> 0 THEN
    FOR lp IN 1..item_check LOOP
      SELECT tst
      INTO xmlU
      FROM
        (SELECT row_number() over (partition BY t.docname order by t.timestamp DESC) rn,
          t.docname,
          XMLQuery('for $i in distinct-values(/invoice/AR_ITEMS['||lp||']/ITEMS/EVENTS/BAL_IMPACTS/DISCOUNT_INFO)
                    where $i = "Plan499 Corp Disc" 
                    or $i = "Plan899 Corp Disc" 
                    or $i = "Plan1099 Corp Disc" 
                    or $i = "Plan1599 Corp Disc"
                    return $i' passing original_xmldoc returning content ).getStringVal() sys_descr,
          SUM(SUBSTR(x.chrg_duration,1,instr(x.chrg_duration,':',1)-1)) over (partition BY t.docname) chrg_duration,
          CASE
            WHEN extractvalue(xmltype('<DISCOUNT_INFO>'||XMLQuery('for $i in distinct-values(/invoice/AR_ITEMS/ITEMS/EVENTS/BAL_IMPACTS/DISCOUNT_INFO)
                              where $i = "Plan499 Corp Disc" 
                              or $i = "Plan899 Corp Disc" 
                              or $i = "Plan1099 Corp Disc" 
                              or $i = "Plan1599 Corp Disc"
                              return $i' passing original_xmldoc returning content )||'</DISCOUNT_INFO>'),'DISCOUNT_INFO') = 'Plan499 Corp Disc' AND SUM(SUBSTR(x.chrg_duration,1,instr(x.chrg_duration,':',1)-1)) over (partition BY t.docname) <= prorate_mins
            THEN insertchildxml(
                  insertchildxml(
                    updatexml(
                      updatexml(
                        updatexml(t.update_xmldoc,'/invoice/BILLINFO/TOTAL_DUE/text()', 
                        extractvalue(t.update_xmldoc,'/invoice/BILLINFO/TOTAL_DUE') - SUM(x.amount) over (partition BY t.docname)),'/invoice/BILLINFO/CURRENT_TOTAL/text()', 
                      extractvalue(t.update_xmldoc,'/invoice/BILLINFO/CURRENT_TOTAL') - (SUM(x.amount) over (partition BY t.docname))),'/invoice/BILLINFO/DISCOUNT_VALUE', 
                    xmltype('<DISCOUNT_VALUE>'||(NVL(extractvalue(t.update_xmldoc,'/invoice/BILLINFO/DISCOUNT_VALUE'),0) - ROUND((SUM(x.amount) over (partition BY t.docname))/1.12,2))||'</DISCOUNT_VALUE>')), '/invoice/AR_ITEMS/ITEMS[NAME="Voice"]/USAGE_RECORDS[STREAM_NAME="NDD"]', 'DURATION_NET_DISCOUNT', 
                xmlelement("DURATION_NET_DISCOUNT", ROUND((SUM(x.amount) over (partition BY t.docname)*-1)/1.12,2))),'/invoice/AR_ITEMS/ITEMS[NAME="Voice"]/USAGE_RECORDS[STREAM_NAME="NDD"]','DURATION_VAT_DISCOUNT',
              xmlelement("DURATION_VAT_DISCOUNT",ROUND(((SUM(x.amount) over (partition BY t.docname)*-1)/1.12)*.12,2)))
              END tst
          FROM xml_billrun_files t ,
          xmltable('/invoice/AR_ITEMS/ITEMS/USAGE_RECORDS/SESSION_INFO' 
                    passing t.update_xmldoc 
                    columns chrg_duration VARCHAR2(20) path 'DURATION', 
                            amount NUMBER path 'AMOUNT') x
        WHERE seq_id  = seq
        AND t.docname = REPLACE(fn,'.','_HULK.')
        )
      WHERE rn = 1;
      UPDATE xml_billrun_files a
      SET update_xmldoc = xmlU
      WHERE seq_id      = seq
      AND docname       = REPLACE(fn,'.','_HULK.');
      COMMIT;
    END LOOP;
    END IF;
END;

1 个答案:

答案 0 :(得分:2)

问题是XPath中的字符串连接。它不像你在做... AR_ITEMS['||lp||']/ITEMS ...

您可以通过lp子句传递PL / SQL passing变量的值,该子句允许多个以逗号分隔的参数;给它一个标识符然后直接在XPath中引用它。我已经使用了"lp"$lp;他们不必匹配PL / SQL变量名称,但如果他们这样做可能会更清楚。

      XMLQuery('for $i in distinct-values(/invoice/AR_ITEMS[$lp]/ITEMS/EVENTS/BAL_IMPACTS/DISCOUNT_INFO)
                where $i = "Plan499 Corp Disc" 
                or $i = "Plan899 Corp Disc" 
                or $i = "Plan1099 Corp Disc" 
                or $i = "Plan1599 Corp Disc"
                return $i' passing original_xmldoc, cast(lp as number) as "lp" returning content ).getStringVal() sys_descr,

PL / SQL循环索引变量lppls_integer,其中传递子句并不喜欢;如果直接传递,则得到ORA-00932,因此需要将其转换为数字数据类型。