PL-SQL
Oracle PL/SQL Insert, Update, Delete & Select Into [Example]
In this tutorial, we are going to learn how to use SQL in PL/SQL. SQL is the actual component that...
"FOR LOOP" statement is best suitable when you want to execute a code for a known number of times rather than based on some other conditions.
In this loop, the lower limit and the higher limit will be specified and as long as the loop variable is in between this range, the loop will be executed.
The loop variable is self-incremental, so no explicit increment operation is needed in this loop. The loop variable need not to be declared, as it is declared implicitly.
FOR <loop_variable> in <lower_limit> .. <higher_limit> LOOP <execution block starts> . . . <execution_block_ends> END LOOP;Syntax Explanation:
Example 1: In this example, we are going to print number from 1 to 5 using FOR loop statement. For that, we will execute the following code.
BEGIN dbms Qutput.put linef.Prp.gram started.' ); FOR a IN 1 .. 5 LOOP dbms_output.put_line(a); END LOOP: dbms_output.put_iine('Program completed.'); END; /
Code Explanation:
The loop statements can also be nested. The outer and inner loop can be of different types. In the nested loop, for every one iteration value of the outer loop, the inner loop will be executed fully.
LOOP -outer <execution block starts> LOOP — inner <execution_part> END LOOP; <execution_block_ends> END LOOP;Syntax Explanation:
Example 1: In this example, we are going to print number from 1 to 3 using FOR loop statement. Each number will be printed as many times as its value. For that, we will execute the following code.
DECLARE b NUMBER; BEGIN dbms output put line(‘Program started' ); FOR a IN 1..3 LOOP b:=1; WHILE (a>=b) LOOP dbms output put line(a); b:=b+1; END LOOP; END LOOP; dbms_output.put_line('Program completed' ); END; /
Code Explanation:
Loop | FOR Loop |
EXIT Criteria | Exit when the counter reaches the limit |
Usage | Good to use when loop count to be executed is known. |
In this tutorial, we are going to learn how to use SQL in PL/SQL. SQL is the actual component that...
What is Package in Oracle? PL/SQL package is a logical grouping of a related subprogram...
What are Loops? Loops allows a certain part of the code in a program to get executed for the...
What is PL/SQL block? In PL/SQL, the code is not executed in single line format, but it is always...
What is PL/SQL? Oracle PL/SQL is an extension of SQL language that combines the data manipulation power...
What is Object Type in PL/SQL? Object-Oriented Programming is especially suited for building...