Oracle PL/SQL - FOR LOOP Statement Bounds

Introduction

The lower and upper bounds of a FOR LOOP statement can be either numeric literals, numeric variables, or numeric expressions.

If a bound does not have a numeric value, then PL/SQL raises the predefined exception VALUE_ERROR.

Demo

SQL>
SQL> DECLARE-- from  ww  w.j a  v a 2s .co  m
  2    first  INTEGER := 1;
  3    last   INTEGER := 10;
  4    high   INTEGER := 100;
  5    low    INTEGER := 12;
  6  BEGIN
  7    -- Bounds are numeric literals:
  8    FOR j IN -5..5 LOOP
  9      NULL;
 10    END LOOP;
 11
 12    -- Bounds are numeric variables:
 13    FOR k IN REVERSE first..last LOOP
 14      NULL;
 15    END LOOP;
 16
 17   -- Lower bound is numeric literal,
 18   -- Upper bound is numeric expression:
 19    FOR step IN 0..(TRUNC(high/low) * 2) LOOP
 20      NULL;
 21    END LOOP;
 22  END;
 23  /

PL/SQL procedure successfully completed.

SQL>

Related Topic