We help IT Professionals succeed at work.

plsql, fetch inner and outer loops

talahi
talahi asked
on
1,322 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

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2008
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Mark GeerlingsDatabase Administrator
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Added points.

Author

Commented:
Thanks for the help. Sorry for the delay, I though I closed this last week.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.