Solved

nested cursor loops

Posted on 2004-08-31
2
979 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle Query - Return results based on minimum value 8 54
pivot rows to columns 1 59
SQL query for highest sequence 4 73
oracle forms question 22 58
Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

738 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