plsql, fetch inner and outer loops

Posted on 2009-07-10
Last Modified: 2013-12-07
I'm not very experienced using plsql, cursors or fetches.

I'm trying to construct an inner and outer loop in plsql.  My question can be summed up by asking if you can use a WHERE clause for the inner fetch.

1.  the outer loop supplies the next value of a WHERE clause to supply to the INNER loop to limit values to search.

2. inner loop users outer loops value in WHERE clause, processes data, the goes back to outer loop for next number

The problem seems to be that the inner loop doesn't finish with the first value from the outer loop but keeps processing data using all the outer loop values without breaking the loop to get the next value from the outer loop.

    CURSOR cur_inner IS

    select * from test_table1

    where x = x;


    CURSOR cur_outer IS

    select * from test_table2;




    OPEN cur_outer;





    FETCH cur_outer

    INTO x, y, z;



    exit fetch_outer_loop when NOT cur_outer%found;


            OPEN cur_inner;







                FETCH cur_inner 

                INTO x, y, z;


/*  How do I limit the inner loop to just to values liited by the WHERE clause in   the CURSOR cur_inner above?  Break the loop and then get the next WHERE clause value from the Outer Loop Cursor.

                  --  process data using values fetched from cur_inne r*/


                exit fetch_inner_loop when NOT cur_inner%found; 



             END LOOP fetch_inner_loop;


             CLOSE cur_inner;



             insert into result_table      (a, b, c )

                                    values (a, b, c );




    END LOOP fetch_outer_loop;

    CLOSE cur_outer_freq;

END test;

Open in new window

Question by:talahi
  • 2
LVL 47

Accepted Solution

schwertner earned 150 total points
ID: 24826602
Cursors can be programmed with parameters:

   || Cursor with parameter list consisting of a single
   || string parameter.
   CURSOR joke_cur (category_in VARCHAR2)
      SELECT name, category, last_used_date
        FROM joke
       WHERE category = UPPER (category_in);

   joke_rec joke_cur%ROWTYPE;

   /* Now when I open the cursor, I also pass the argument */
   OPEN joke_cur (:joke.category);
   FETCH joke_cur INTO joke_rec;

See here
LVL 34

Assisted Solution

by:Mark Geerlings
Mark Geerlings earned 150 total points
ID: 24826918
Yes, one option is to declare and call cursors with parameters.  The other options is to declare vairable(s) in your inner cursor that will br populated by value(s0 from your outer cursor.

Here is an example:

   v_outer_key  varchar2(100);  
   v_inner_row  test_table2%rowtype;

    CURSOR cur_inner IS
    select [key_column] from test_table1
    where [key_column] = v_outer_key;
    CURSOR cur_outer IS
    select * from test_table2;
    OPEN cur_outer;
      open cur_inner;
        fetch cur_inner into v_inner_row;
        exit when cur_inner%notfound;
        -- do whatever processing you want of the "inner" records here
      end loop;
      close cur_inner;
    end loop;
    close cur_outer;

1. Replace [key_column] with a valid column name in your table
2. I think this declaration ( v_inner_row  test_table2%rowtype) is correct, but i rarely use that syntax.  I usually declare and use separate values for each column being selected.

Author Comment

ID: 24885972
Added points.

Author Closing Comment

ID: 31602225
Thanks for the help. Sorry for the delay, I though I closed this last week.

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
compre toata in where clue oracle 4 41
Oracle Syntax 8 41
query in Oracle forms Builder 2 27
Oracle and DateTime math 6 7
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
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…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

705 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now