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 35

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle regular expression 6 47
Need a replacement data type in Oracle 6 65
case statement in where clause 5 42
Converting a row into a column 2 44
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
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 set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

920 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

14 Experts available now in Live!

Get 1:1 Help Now