2016-02-26 44 views
1

如何在xmltable的xpath中傳遞變量值?如何在xmltable中傳遞值

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

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; 
+0

但在這樣做,用FOR循環,生病得到錯誤'錯誤(674,46):PL/SQL:ORA-00932:不一致的數據類型:預期 - 得到NATIVE INTEGER' – NelzKi

+0

@ NelzKi - 是的,對上一個問題的回答是錯誤的,我已經糾正了這兩個問題。您需要將循環變量轉換爲SQL數字類型。 –

+0

我明白了..謝謝你的幫助..:D – NelzKi