1. Basic cycle
End with end loop starting with loop. To avoid endless loop, you must use the exit or exit when statement.
Create Table temp (cola int );
Declare
I INT: = 1;
Begin
Loop
Insert into temp values (I );
Exit when I = 10;
I: = I + 1;
End loop;
End;
2. While Loop
Start with while loop and end with end loop;
Declare
I INT: = 1;
Begin
While I <= 10 Loop
Insert into temp values (I );
I: = I + 1;
End loop;
End;
3. For Loop syntax
For counter in [reverse]
Lower_bound .. upper_bound Loop
Statement1;
Statement2;
..........
End loop;
By default, when the for loop is used, 1 is automatically added for each variable. If the reverse option is specified, the loop control variable automatically
Minus 1.
Begin
For I in reverse 1 .. 10 Loop
Insert into temp values (I );
End loop;
End;
4. nested loops and labels
You can use <label_name> to define labels for nested loops.
Declare
Result1 int;
Begin
<Outerr>
For I in 1 .. 100 Loop
<Inter>
For J in 1 .. 100 Loop
Result1: = I * J;
Exit outerr when result1 = 1000;
Exit when result1= 500;
End loop Inter;
Dbms_output.put_line (result1 );
End loop outerr;
Dbms_output.put_line (result1 );
End;