Syntax using FOR LOOP in Oracle PL/SQL

FOR LOOP example in Oracle PL/SQL

FOR LOOP is used to execute your code repeatedly for a fixed number of time as you specified.

Syntax of using FOR LOOP:

FOR <counter> IN [REVERSE] <low_number>..<high_number>
LOOP
   <statements>
END LOOP;

Example of using FOR LOOP in PL/SQL Block:

-- Use of FOR LOOP in example 
SET SERVEROUTPUT ON
DECLARE 
   v_id number(2); 
BEGIN 
   v_id := 1;
   FOR v_id in 1 .. 20 
   LOOP 
		dbms_output.put_line('id: ' || v_id);
		v_id := V_id + 1;		
   END LOOP; 
END; 
/

-- Use of REVERSE, its start from 20 to 1;
SET SERVEROUTPUT ON
DECLARE 
   v_id number(2); 
BEGIN 
   v_id := 20;;
   FOR v_id in REVERSE 1 .. 20 
   LOOP 
		dbms_output.put_line('id: ' || v_id); 
		v_id := v_id - 1;
   END LOOP; 
END; 
/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.