Solved

plsql, fetch inner and outer loops

Posted on 2009-07-10
4
1,235 Views
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.
CREATE OR REPLACE PROCEDURE test AS
 
 
    CURSOR cur_inner IS
    select * from test_table1
    where x = x;
    
    CURSOR cur_outer IS
    select * from test_table2;
   
 
BEGIN
 
    OPEN cur_outer;
 
    
     <<fetch_outer_loop>>
    
    LOOP 
    FETCH cur_outer
    INTO x, y, z;
    
    
    exit fetch_outer_loop when NOT cur_outer%found;
          
 
            OPEN cur_inner;
    
            <<fetch_inner_loop>>
    
            LOOP 
           
    
                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 );
                                       
             commit;
          
    END LOOP fetch_outer_loop;
 
    CLOSE cur_outer_freq;
 
END test;

Open in new window

0
Comment
Question by:talahi
[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
4 Comments
 
LVL 48

Accepted Solution

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

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

   joke_rec joke_cur%ROWTYPE;

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

See here

http://docstore.mik.ua/orelly/oracle/prog2/ch06_10.htm
0
 
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:

CREATE OR REPLACE PROCEDURE test AS
 
   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;
   
BEGIN
 
    OPEN cur_outer;
    loop
      open cur_inner;
      loop
        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;
end;
/

Notes:
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.
0
 

Author Comment

by:talahi
ID: 24885972
Added points.
0
 

Author Closing Comment

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

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
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 how to recover a database from a user managed backup
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

695 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