Link to home
Start Free TrialLog in
Avatar of talahi
talahi

asked on

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.
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

ASKER CERTIFIED SOLUTION
Avatar of schwertner
schwertner
Flag of Antarctica image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of talahi
talahi

ASKER

Added points.
Avatar of talahi

ASKER

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