Examples of PL/SQL FOR Loop
Example: Print Number From 1 to 5 Using FOR Loop in PL/SQL
Query:
SET SERVEROUTPUT ON;
DECLARE
counter NUMBER;
BEGIN
DBMS_OUTPUT.PUT_LINE('PL/SQL FOR LOOP EXECUTION');
FOR counter IN 1..5 LOOP
DBMS_OUTPUT.PUT_LINE('COUNTER VALUE: '|| counter);
END LOOP;
END;
/
Output:
Explanation:
SET SERVEROUTPUT ON is used to display the output. Loop variable i.e. ‘counter ‘ is declared in declaration block.Loop variable can be used directly without declaring it.FOR loop is declared with start value as 1 and end value as 5. The END LOOP keyword is used to end the loop.At last,END keyword is used to terminate the execution.Number from 1 to 5 are printed along with the text information using for loop.
PL/SQL For Loop
PL/SQL stands for Procedural Language/ Structured Query Language. It has block structure programming features. With PL/SQL, you can fetch data from the table, add data to the table, make decisions, perform repetitive tasks, and handle errors.PL/SQL supports SQL queries. PL/SQL contains declaration block, execution block, and exception block. Declare and exception blocks are optional.
In this article, we delve into the versatility of the PL/SQL FOR loop, a key construct for procedural programming in Oracle databases. We’ll explore its syntax, provide examples of its application, demonstrate the use of the REVERSE keyword for reverse iteration, and discuss the effectiveness of nested FOR loops.