Nested for loop vs table join in for loop : For Loop « PL SQL « Oracle PL / SQL






Nested for loop vs table join in for loop

    

SQL>
SQL>
SQL> create table myTable1( a int primary key, y char(80) );

Table created.

SQL>
SQL> create table myTable2( b int primary key, a references myTable1, y char(80) );

Table created.

SQL>
SQL> create index myTable2_a_idx on myTable2(a);

Index created.

SQL>
SQL> create table myTable3( c int primary key, b references myTable2, y char(80) );

Table created.

SQL>
SQL> create index myTable3_b_idx on myTable3(b);

Index created.

SQL>
SQL> insert into myTable1
  2  select rownum, 'x'
  3  from all_objects
  4  where rownum <= 1000;

1000 rows created.

SQL>
SQL> insert into myTable2
  2  select rownum, mod(rownum,1000)+1, 'x'
  3  from all_objects
  4  where rownum <= 5000;

5000 rows created.

SQL>
SQL> insert into myTable3
  2  select rownum, mod(rownum,5000)+1, 'x'
  3  from all_objects;

12588 rows created.

SQL>
SQL> begin
  2      for i in 1 .. 1000
  3      loop
  4          for x in ( select myTable1.a myTable1a, myTable1.y myTable1y,
  5                            myTable2.b myTable2b, myTable2.a myTable2a, myTable2.y myTable2y,
  6                            myTable3.c myTable3c, myTable3.b myTable3b, myTable3.y myTable3y
  7                       from myTable1, myTable2, myTable3
  8                      where myTable1.a = i
  9                        and myTable2.a (+) = myTable1.a
 10                        and myTable3.b (+) = myTable2.b )
 11          loop
 12              null;
 13          end loop;
 14      end loop;
 15  end;
 16  /

PL/SQL procedure successfully completed.

SQL>
SQL>
SQL> begin
  2      for i in 1 .. 1000
  3      loop
  4          for a in ( select myTable1.a, myTable1.y from myTable1 where myTable1.a = i )
  5          loop
  6              for b in ( select myTable2.b, myTable2.a, myTable2.y from myTable2 where myTable2.a = a.a )
  7              loop
  8                  for c in ( select myTable3.c, myTable3.b, myTable3.y from myTable3 where myTable3.b = b.b )
  9                  loop
 10                      null;
 11                  end loop;
 12              end loop;
 13          end loop;
 14      end loop;
 15  end;
 16  /

PL/SQL procedure successfully completed.

SQL>

   
    
    
    
  








Related examples in the same category

1.Your first FOR loop
2.For loop: counter IN 1..5
3.Nesting FOR loops
4.REVERSE: Reversing the loop
5.Changing the loop increment
6.Use variable as an upper bound of for loop
7.Exit(break) a for loop
8.Call EXIT to exit a for loop
9.Call EXIT WHEN to exit a function
10.Put DBMS_OUTPUT.PUT_LINE in for loop
11.If... End if
12.A numeric FOR loop with insert statement
13.Use for counter in insert statement
14.The scope of the index of a FOR LOOP.
15.FOR Loop Ranges with variable
16.FOR Loop Scoping Rules
17.Numeric FOR Loop
18.Loop till count(*)
19.Use for loop as if statement
20.Use for loop to loop through result from a select statement
21.Define a looping indexer as member variable in a procedure
22.Numeric loop will ignore the externally scoped variable and create a new locally scoped variable.
23.Nested for loop
24.loop index scope is limited to the FOR loop.
25.starting_number and ending_number must be integers.
26.For each reverse