如何在xmltable中传递值

时间:2016-02-26 11:25:50

标签: sql xml oracle

如何在xmltable的xpath中传递变量值?

DECLARE
  v NUMBER := 0;
BEGIN
  SELECT *
  FROM xml_billrun_files t ,
    xmltable('/invoice/AR_ITEMS[@elem='||v||']/ITEMS/USAGE_RECORDS/SESSION_INFO' 
              passing t.update_xmldoc 
              columns chrg_duration VARCHAR2(20) path 'DURATION', 
                      amount NUMBER path 'AMOUNT') x;
END;

我试过这个。

DECLARE
  v NUMBER := 0;
BEGIN
  SELECT *
  FROM xml_billrun_files t ,
    xmltable('/invoice/AR_ITEMS[@elem=$i]/ITEMS/USAGE_RECORDS/SESSION_INFO' 
              passing t.update_xmldoc, xmltype(v) as "i"
              columns chrg_duration VARCHAR2(20) path 'DURATION', 
                      amount NUMBER path 'AMOUNT') x;
END;

但它返回错误:

ORA-31011: XML parsing failed
ORA-19202: Error occurred in XML processing
LPX-00210: expected '<' instead of '0'
Error at line 1
ORA-06512: at "SYS.XMLTYPE", line 310
ORA-06512: at line 1
ORA-06512: at line 5
31011. 00000 -  "XML parsing failed"
*Cause:    XML parser returned an error while trying to parse the document.
*Action:   Check if the document to be parsed is valid.

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

在前一个问题的my answer中,我错误地将xmltype(lp) as "lp"用于您的XMLQuery调用。不知道为什么它没有在那里抱怨,但它实际上并没有限制比赛......

对于此XMLTable调用,您可以直接传递数字,而无需转换/转换,因为它已经是一个数字:

  SELECT chrg_duration, amount
  into ...
  FROM xml_billrun_files t ,
    xmltable('/invoice/AR_ITEMS[@elem=$i]/ITEMS/USAGE_RECORDS/SESSION_INFO' 
              passing t.update_xmldoc, v as "i"
              columns chrg_duration VARCHAR2(20) path 'DURATION', 
                      amount NUMBER path 'AMOUNT') x;

如果您在for循环中执行此操作,则索引是错误的数据类型,您需要将其强制转换为数字:

  SELECT chrg_duration, amount
  into ...
  FROM xml_billrun_files t ,
    xmltable('/invoice/AR_ITEMS[@elem=$i]/ITEMS/USAGE_RECORDS/SESSION_INFO' 
              passing t.update_xmldoc, cast(v as number) as "i"
              columns chrg_duration VARCHAR2(20) path 'DURATION', 
                      amount NUMBER path 'AMOUNT') x;