In PL/SQL, FOR loops are a powerful control structure used to iterate over a defined range of numbers or rows. However, this construct has specific syntactic rules. One common error developers encounter is:

PLS-00364: Loop index variable <name> must be an integer

This article explains why this error occurs, provides examples, and offers clear solutions to fix it.

What Causes PLS-00364?

The error occurs when the loop index variable in a FOR loop is either:

  • Explicitly declared as a non-integer type (like NUMBER, FLOAT, DECIMAL, VARCHAR2, etc.)
  • Or, when the range provided in the loop is not an integer range

PL/SQL requires that FOR loop index variables are always implicitly created and treated as integers. You cannot use decimal values or user-declared non-integer loop counters in a FOR loop.

Examples That Produce PLS-00364

Example 1: Loop Range with Decimal Values

BEGIN
FOR i IN 1.1 .. 5.1 LOOP
DBMS_OUTPUT.PUT_LINE(i);
END LOOP;
END;
/

Error Message:

PLS-00364: loop index variable 'I' must be an integer

Example 2: Loop Index Variable Declared as NUMBER

DECLARE
i NUMBER := 1;
BEGIN
FOR i IN 1 .. 5 LOOP
DBMS_OUTPUT.PUT_LINE(i);
END LOOP;
END;
/

Why this fails: Even though i is declared as NUMBER, the compiler does not allow this conflict because the loop control variable is implicitly managed by the FOR loop construct.

Correct Usage: Use Integer Values Only

Solution 1: Use Integer Range Without Declaring the Index

BEGIN
FOR i IN 1 .. 5 LOOP
DBMS_OUTPUT.PUT_LINE('Value: ' || i);
END LOOP;
END;
/

This works correctly because:

  • The index i is implicitly declared
  • The range is a pair of integer literals

Solution 2: Use Integer Variables in Range

DECLARE
v_start INTEGER := 1;
v_end INTEGER := 5;
BEGIN
FOR i IN v_start .. v_end LOOP
DBMS_OUTPUT.PUT_LINE('Value: ' || i);
END LOOP;
END;
/

Again, the range resolves to integers, and the loop variable i is not explicitly declared.

Solution 3: Use WHILE Loop for Non-Integer Increments

If your logic involves non-integer increments (e.g., 0.5 or 2.7), a FOR loop cannot be used. Instead, use a WHILE loop with a floating-point variable.

DECLARE
v_counter NUMBER := 1.5;
BEGIN
WHILE v_counter <= 5.0 LOOP
DBMS_OUTPUT.PUT_LINE('Counter: ' || v_counter);
v_counter := v_counter + 0.5;
END LOOP;
END;
/

Why this works: WHILE loops allow more flexibility with data types and control logic, unlike FOR loops that enforce integer control variables.

Summary: Do’s and Don’ts

DoDon’t
Use implicit loop variables (FOR i IN...)Declare loop index as NUMBER, FLOAT, etc.
Use INTEGER, PLS_INTEGER, or literalsUse decimals like 1.1 .. 5.5 in loop range
Use WHILE loop for decimal countersExpect FOR loop to handle non-integer logic

Conclusion

The PLS-00364 error is a strict PL/SQL constraint that enforces loop index variables to be integers only within FOR loops. To fix this:

  • Let Oracle implicitly create the index variable
  • Ensure loop bounds are integers
  • Use WHILE loops if your logic needs floating-point arithmetic

By following these guidelines, your loops will compile cleanly and behave as expected.