Solved

sqlplus nested loop problem (Urgent please!)

Posted on 2004-09-17
2
834 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
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.
Suggested Courses

632 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