Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
PL/SQL Part 5
1. P L / S Q L T u t o r i a l - B y E r G u r p r e e t S i n g h
Visit: http://gsbprogramming.blogspot.in
2. P L / S Q L T u t o r i a l - B y E r G u r p r e e t S i n g h
Visit: http://gsbprogramming.blogspot.in
Iterative or Looping statements in PL/SQL
Basic structure of a Looping statement is:
PL/SQL provides following Looping statements:
BASIC LOOP
WHILE LOOP
FOR LOOP
Nested Loops
3. P L / S Q L T u t o r i a l - B y E r G u r p r e e t S i n g h
Visit: http://gsbprogramming.blogspot.in
BASIC LOOP
Syntax:
LOOP
<Looping statements>
END LOOP;
Note: EXIT or EXIT WHEN is used for breaking the loop
Condition must be that statement which results a Boolean value i.e. either true or false
For example
DECLARE
vCount number :=1;
BEGIN
LOOP
dbms_output.put(vCount||‘ ’);
vCount:=vCount + 1;
EXIT WHEN vCount=5;
END LOOP;
dbms_output.new_line;
END;
Will print:
1 2 3 4
dbms_output.new_line; is used to flush the output, otherwise your output will not be shown
WHILE LOOP
Syntax:
WHILE condition LOOP
<Looping statements>
END LOOP;
Condition must be that statement which results a Boolean value i.e. either true or false
For example:
DECLARE
vCount number :=1;
BEGIN
WHILE vCount!=5 LOOP
dbms_output.put(vCount||‘ ’);
vCount:=vCount + 1;
END LOOP;
dbms_output.new_line;
END;
4. P L / S Q L T u t o r i a l - B y E r G u r p r e e t S i n g h
Visit: http://gsbprogramming.blogspot.in
Will print:
1 2 3 4
FOR LOOP
Syntax:
FOR counter IN start_value . . end_value LOOP
<Looping statements>
END LOOP;
Counter must have value between start_value and end_value, only then looping statements will
execute. You cannot update the value of counter
For example
BEGIN
FOR vCount IN 1 .. 4 LOOP
dbms_output.put(vCount||‘ ’);
END LOOP;
dbms_output.new_line;
END;
Will print:
1 2 3 4
Nested LOOPs
Loops can also be nested in each other. For example:
Condition must be that statement which results a Boolean value i.e. either true or false
For example
BEGIN
FOR i IN 1 .. 4 LOOP
FOR j IN 1 .. i LOOP
dbms_output.put(i||‘ ’);
END LOOP;
dbms_output.new_line;
END LOOP;
END;
Will print:
1
2 2
3 3 3
4 4 4 4