nested cursor loops

Posted on 2004-08-31
Medium Priority
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.

Question by:wizard2000
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 35

Accepted Solution

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

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


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

Expert Comment

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

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
      END LOOP;

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that useā€¦
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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 video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
Suggested Courses

762 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