Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sqlplus nested loop problem (Urgent please!)

Posted on 2004-09-17
2
Medium Priority
?
835 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 600 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines

670 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