Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Jerry.

Asked: July 31, 2017 - 12:34 pm UTC

Last updated: August 01, 2017 - 2:34 am UTC

Version: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

Viewed 1000+ times

You Asked

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!

and Connor said...

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]',

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Thanks!

Jerry, July 31, 2017 - 1:22 pm UTC

Case-sensitivity will be the death of me. Thanks, and I owe you a beer.
Connor McDonald
August 01, 2017 - 2:34 am UTC

glad we could help

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library