plsql, fetch inner and outer loops

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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mark GeerlingsDatabase AdministratorCommented:
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.
talahiAuthor Commented:
Added points.
talahiAuthor Commented:
Thanks for the help. Sorry for the delay, I though I closed this last week.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.