PL/PGSQL - Control Structures (Loops)
PL/PGSQL - Control Structures (Loops)
Simple Loops
With the LOOP, EXIT, CONTINUE, WHILE, and FOR statements, you can arrange for your PL/pgSQL
function to repeat a series of commands.
1. LOOP
[ <<label>> ]
LOOP
statements
END LOOP [ label ];
LOOP defines an unconditional loop that is repeated indefinitely until terminated by an EXIT or
RETURN statement. The optional label can be used by EXIT and CONTINUE statements within
nested loops to specify which loop those statements refer to.
2. EXIT
EXIT [ label ] [ WHEN boolean-expression ];
If no label is given, the innermost loop is terminated and the statement following END LOOP is
executed next. If label is given, it must be the label of the current or some outer level of nested loop
or block. Then the named loop or block is terminated and control continues with the statement after the
loop's/block's corresponding END.
If WHEN is specified, the loop exit occurs only if boolean-expression is true. Otherwise, control
passes to the statement after EXIT.
EXIT can be used with all types of loops; it is not limited to use with unconditional loops.
When used with a BEGIN block, EXIT passes control to the next statement after the end of the block.
Note that a label must be used for this purpose; an unlabelled EXIT is never considered to match a
BEGIN block.
Examples:
LOOP
-- some computations
IF count > 0 THEN
EXIT; -- exit loop
END IF;
END LOOP;
Page 1
PL/PgSQL Tutorial 3
LOOP
-- some computations
EXIT WHEN count > 0; -- same result as previous example
END LOOP;
<<ablock>>
BEGIN
-- some computations
IF stocks > 100000 THEN
EXIT ablock; -- causes exit from the BEGIN block
END IF;
-- computations here will be skipped when stocks > 100000
END;
3. CONTINUE
If no label is given, the next iteration of the innermost loop is begun. That is, all statements
remaining in the loop body are skipped, and control returns to the loop control expression (if any) to
determine whether another loop iteration is needed. If label is present, it specifies the label of the
loop whose execution will be continued.
If WHEN is specified, the next iteration of the loop is begun only if boolean-expression is true.
Otherwise, control passes to the statement after CONTINUE.
CONTINUE can be used with all types of loops; it is not limited to use with unconditional loops.
Examples:
LOOP
-- some computations
EXIT WHEN count > 100;
CONTINUE WHEN count < 50;
-- some computations for count IN [50 .. 100]
END LOOP;
4. WHILE
[ <<label>> ]
WHILE boolean-expression LOOP
statements
END LOOP [ label ];
Page 2
PL/PgSQL Tutorial 3
5. FOR LOOP
[ <<label>> ]
FOR name IN [ REVERSE ] expression .. expression [ BY expression ]
LOOP
statements
END LOOP [ label ];
This form of FOR creates a loop that iterates over a range of integer values. The variable name is
automatically defined as type integer and exists only inside the loop (any existing definition of the
variable name is ignored within the loop).
The two expressions giving the lower and upper bound of the range are evaluated once when entering
the loop. If the BY clause isn't specified the iteration step is 1, otherwise it's the value specified in the
BY clause, which again is evaluated once on loop entry. If REVERSE is specified then the step value is
subtracted, rather than added, after each iteration.
Some examples of integer FOR loops:
If the lower bound is greater than the upper bound (or less than, in the REVERSE case), the loop body
is not executed at all. No error is raised.
If a label is attached to the FOR loop then the integer loop variable can be referenced with a qualified
name, using that label.
Page 3