Solved

nested cursor loops

Posted on 2004-08-31
2
980 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
[X]
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
2 Comments
 
LVL 35

Accepted Solution

by:
johnsone earned 250 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
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.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

728 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