?
Solved

nested cursor loops

Posted on 2004-08-31
2
Medium Priority
?
983 Views
Last Modified: 2012-05-05
I have a problem in that i need to create a nested loop in PL/SQL. I basically need to use information from the first loop which uses a cursor to select information in the second loop, which also uses a cursor. As it stands i can not get it working. Any help would be appreciated, especially examples.

Cheers
0
Comment
Question by:wizard2000
2 Comments
 
LVL 35

Accepted Solution

by:
johnsone earned 1000 total points
ID: 11942255
You need to use a paramterized cursor, like this:


declare
   cursor outer is
     select col1
        from tab1;
   cursor inner(v_col1 tab1.col1%type)
     select col2
        from tab2
       where col3 = v_col1;

begin

   for outer_rec in outer loop
     for inner_rec in inner(outer_rec.col1) loop
       ...
     end loop;
  end loop;
end;
0
 
LVL 7

Expert Comment

by:grim_toaster
ID: 11942282
Here's a very simple example of using an inner and outer cursor for loop:

BEGIN
FOR rec1 IN (SELECT table_name FROM user_tables) LOOP
      FOR rec2 IN (SELECT column_name FROM user_tab_columns WHERE table_name = rec1.table_name AND column_id = 1) LOOP
            DBMS_OUTPUT.put_line(rec2.column_name);      
      END LOOP;
END LOOP;
END;

If you have any particular problems, perhaps we could help you with them?
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses
Course of the Month16 days, 9 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question