0

Whats wrong with following nested loop, it throws following error:

ORA-00936: missing expression

SET SERVEROUT ON;
SET FEED OFF;
DECLARE
schema_name varchar2(100);
table_name varchar2(100);

    BEGIN

        FOR outer_rec IN (select * from  scott.table_list) LOOP  

            FOR inner_rec IN (select a.* from all_tab_columns a where a.column_name =outer_rec.table_name(+) and a.owner='SCOTT' order by a.table_name) LOOP
            dbms_output.put_line (q'[||]' ||inner_rec.column_name || q'[||'|@@@@|']');
            END LOOP;

        END LOOP;

    END;
/
4
  • reomve (+) in a.column_name =outer_rec.table_name(+) Commented Mar 3, 2014 at 13:01
  • But Why? I want all the columns from all_tab_columns. Anyways, removing (+) does not give any output. Commented Mar 3, 2014 at 13:05
  • this (+) is important, it allows an outer join in case outer_rec.table_name has no match Commented Mar 3, 2014 at 13:06
  • you can have Outer Join only if you are using to join two tables. here "outer_rec.table_name" is variable to query . Not getting out put is because you are trying to put table name in column name. May be your intention is "a.table_name =outer_rec.table_name" Commented Mar 3, 2014 at 13:11

3 Answers 3

1

Since you're using two separate cursors there's really no way to outer join the results together. A better way to handle this would be to use a single cursor that does what you need to have done, as in:

BEGIN
  FOR aRow IN (select *
                 FROM scott.table_list t
                 LEFT OUTER JOIN all_tab_columns a
                   ON (a.owner = 'SCOTT' AND
                       a.table_name = t.table_name)
                 order by a.table_name)
  LOOP
    dbms_output.put_line (q'[||]' || aRow.column_name || q'[||'|@@@@|']');
  END LOOP;
END;

Note that this also uses the ANSI syntax for a left outer join which is preferred over the old Oracle-style ((+)) syntax.

Share and enjoy.

Sign up to request clarification or add additional context in comments.

Comments

0

I would write it this way (also, your variables are not used):

SET SERVEROUT ON;
SET FEED OFF;
DECLARE
  schema_name VARCHAR2(100);
  table_name  VARCHAR2(100);

  CURSOR c_get_tables IS
    SELECT * FROM scott.table_list;

  CURSOR c_get_all(c_name_in IN scott.table_name%TYPE) IS
    SELECT a.*
      FROM all_tab_columns a
     WHERE a.table_name = c_name_in(+)
       AND a.owner = 'SCOTT'
     ORDER BY a.table_name;

BEGIN

  FOR outer_rec IN c_get_tables
  LOOP

    FOR inner_rec IN c_get_all(outer_rec.table_name)
    LOOP
      dbms_output.put_line(q'[||]' || inner_rec.column_name ||
                           q'[||'|@@@@|']');
    END LOOP;

  END LOOP;

END;
/

Comments

0

try this

SET SERVEROUT ON;
SET FEED OFF;
DECLARE
schema_name varchar2(100);
table_name varchar2(100);

BEGIN

    FOR outer_rec IN (select * from  scott.table_list) LOOP  

        FOR inner_rec IN (select a.* from all_tab_columns a where a.table_name =outer_rec.table_name and a.owner='SCOTT' order by a.table_name) LOOP
        dbms_output.put_line (q'[||]' ||inner_rec.column_name || q'[||'|@@@@|']');
        END LOOP;

    END LOOP;

END;

/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.