Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How can I iterate over the columns of a REF CURSOR?

694243Dec 17 2009 — edited Dec 17 2009
I have the following situation:
DECLARE
   text   VARCHAR2 (100) := '';
   
   TYPE gen_cursor is ref cursor;
   c_gen gen_cursor;

   CURSOR c_tmp
   IS
        SELECT   *
          FROM   CROSS_TBL
      ORDER BY   sn;
BEGIN
   FOR tmp IN c_tmp
   LOOP
      text := 'select * from ' || tmp.table_name || ' where seqnum = ' || tmp.sn;

      OPEN c_gen FOR text;

      -- here I want to iterate over the columns of c_gen
      -- c_gen will have different number of columns every time,
      --        because we select from a different table
      -- I have more than 500 tables, so I cannot define strong REF CURSOR types!

      -- I need something like
      l := c_gen.columns.length;
      for c in c_gen.columns[1]..c_gen.columns[l]
      LOOP
          -- do something with the column value
      END LOOP;
   END LOOP;
END;
As you can see from the comments in the code, I couln'd find any examples on the internet with weak REF CURSORS and selecting from many tables.
What I found was:
CREATE PACKAGE admin_data AS
   TYPE gencurtyp IS REF CURSOR;
   PROCEDURE open_cv (generic_cv IN OUT gencurtyp, choice INT);
END admin_data;
/
CREATE PACKAGE BODY admin_data AS
   PROCEDURE open_cv (generic_cv IN OUT gencurtyp, choice INT) IS
   BEGIN
      IF choice = 1 THEN
         OPEN generic_cv FOR SELECT * FROM employees;
      ELSIF choice = 2 THEN
         OPEN generic_cv FOR SELECT * FROM departments;
      ELSIF choice = 3 THEN
         OPEN generic_cv FOR SELECT * FROM jobs;
      END IF;
   END;
END admin_data;
/
But they have only 3 tables here and I have like 500. What can I do here?

Thanks in advance for any help!
This post has been answered by Asheesh Ambardar on Dec 17 2009
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 14 2010
Added on Dec 17 2009
6 comments
6,539 views