Solved

sqlplus nested loop problem (Urgent please!)

Posted on 2004-09-17
2
831 Views
Last Modified: 2008-01-16
The nested loop is not working!!!
The outer loop is executing only once.Inner loop is executing fine...
What is the problem??
=============
declare
v1 VARCHAR2(10);
v2 VARCHAR2(30);
v3 integer;
v4 integer;
cursor c1 is
        select f1 from tab1;
cursor c2 is
        select f2 from tab2;

begin
open c1;
open c2;
<<loop1>>
loop
fetch c1 into v1;
exit when c1%NOTFOUND;
<<loop2>>
loop
fetch c2 into v2;
exit when c2%NOTFOUND;
insert into tab3(ID, FAMILY) values (v1, v2);

end loop loop2;
end loop loop1;
close c1;
close c2;
end;
/
====================
0
Comment
Question by:tooki
[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 Comments
 
LVL 7

Accepted Solution

by:
BobMc earned 150 total points
ID: 12089102
You open both cursors at the top and fetch rows from them - first time around the outer loop, this is fine.
Second time around, you have already exhausted c2, so it will always return no more rows.

You need to close c2 and reopen it for each inner loop

declare
v1 VARCHAR2(10);
v2 VARCHAR2(30);
v3 integer;
v4 integer;
cursor c1 is
        select f1 from tab1;
cursor c2 is
        select f2 from tab2;

begin
open c1;
<<loop1>>
loop
  fetch c1 into v1;
  exit when c1%NOTFOUND;
  <<loop2>>
  open c2;
  loop
    fetch c2 into v2;
    exit when c2%NOTFOUND;
    insert into tab3(ID, FAMILY) values (v1, v2);
  end loop loop2;
  close c2;
end loop loop1;
close c1;
end;
/
0
 

Author Comment

by:tooki
ID: 12089227
Many thanks!!!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

749 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