I'm getting PLS-00302 when trying to select from a chunk of XML using XMLTYPE and XMLTABLLE.
This works, and I get the desired output...
SELECT xt.*
FROM (SELECT xmltype (
'<TABLE>
<TR><TD>Organization</TD><TD>Organization Title</TD><TD>Account</TD><TD>Account Title</TD></TR>
<TR><TD>01</TD><TD>Pooled</TD><TD>1234</TD><TD>My Account</TD></TR>
</TABLE>')
xml_data
FROM DUAL) x,
XMLTABLE ('/TABLE/TR'
PASSING x.xml_data
COLUMNS "td1" VARCHAR2 (100) PATH 'TD[1]',
"td2" VARCHAR2 (100) PATH 'TD[2]',
"td3" VARCHAR2 (100) PATH 'TD[3]',
"td4" VARCHAR2 (100) PATH 'TD[4]') xt;
The output looks like this...
td1 td2 td3 td4
Organization Organization Title Account Account Title
01 Pooled 1234 My Account
But when I try to put that SQL into PL/SQL, I get the 302 error...
DECLARE
lv1 VARCHAR2 (100);
lv2 VARCHAR2 (100);
lv3 VARCHAR2 (100);
lv4 VARCHAR2 (100);
lvc CLOB
:= '
<TABLE>
<TR><TD>Organization</TD><TD>Organization Title</TD><TD>Account</TD><TD>Account Title</TD></TR>
<TR><TD>01</TD><TD>Pooled</TD><TD>1234</TD><TD>My Account</TD></TR>
</TABLE>';
BEGIN
FOR myrec
IN (SELECT xt.*
FROM (SELECT xmltype (lvc) xml_data FROM DUAL) x,
XMLTABLE ('/TABLE/TR'
PASSING x.xml_data
COLUMNS "td1" VARCHAR2 (100) PATH 'TD[1]',
"td2" VARCHAR2 (100) PATH 'TD[2]',
"td3" VARCHAR2 (100) PATH 'TD[3]',
"td4" VARCHAR2 (100) PATH 'TD[4]') xt)
LOOP
DBMS_OUTPUT.put_line (
'MYREC-->'
|| myrec.td1
|| ':'
|| myrec.td2
|| ':'
|| myrec.td3
|| ':'
|| myrec.td4
);
END LOOP;
END;
When I comment the references to the columns, the loop completes without error...
-- || myrec.td1
-- || ':'
-- || myrec.td2
-- || ':'
-- || myrec.td3
-- || ':'
-- || myrec.td4
How can I reference those columns in the cursor for loop?
Thanks!
Check the output of that SQL carefully :-)
SQL> SELECT xt.*
2 FROM (SELECT xmltype (
3 '<TABLE>
4 <TR><TD>Organization</TD><TD>Organization Title</TD><TD>Account</TD><TD>Account Title</TD></TR>
5 <TR><TD>01</TD><TD>Pooled</TD><TD>1234</TD><TD>My Account</TD></TR>
6 </TABLE>')
7 xml_data
8 FROM DUAL) x,
9 XMLTABLE ('/TABLE/TR'
10 PASSING x.xml_data
11 COLUMNS "td1" VARCHAR2 (100) PATH 'TD[1]',
12 "td2" VARCHAR2 (100) PATH 'TD[2]',
13 "td3" VARCHAR2 (100) PATH 'TD[3]',
14 "td4" VARCHAR2 (100) PATH 'TD[4]') xt;
td1
----------------------------------------------------------------------------------------------------
td2
----------------------------------------------------------------------------------------------------
td3
----------------------------------------------------------------------------------------------------
td4
----------------------------------------------------------------------------------------------------
Organization
Organization Title
Account
Account Title
01
Pooled
1234
My Account
See the columns are not "TD1" etc, they are "td1". They are *lowercase*.
So you need to reference them in your plsql code as quoted identifiers, ie, myrec."td1"
Or you can change your SQL to be something like:
COLUMNS TD1 VARCHAR2 (100) PATH 'TD[1]',