Solved

sqlplus nested loop problem (Urgent please!)

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
Via a live example, show how to take different types of Oracle backups using RMAN.
This video shows how to recover a database from a user managed backup

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now