0% found this document useful (0 votes)
88 views

PL/PGSQL - Control Structures (Loops)

This document discusses control structures for loops in PL/PgSQL including simple loops with LOOP, EXIT, CONTINUE, WHILE, and FOR statements. LOOP defines an unconditional loop repeated until an EXIT or RETURN statement. EXIT terminates the current or outer loop. CONTINUE skips to the next loop iteration. WHILE repeats a sequence while a boolean expression is true. FOR loops over a range of integer values, optionally in reverse order or with a step value.

Uploaded by

Aali Yah
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

PL/PGSQL - Control Structures (Loops)

This document discusses control structures for loops in PL/PgSQL including simple loops with LOOP, EXIT, CONTINUE, WHILE, and FOR statements. LOOP defines an unconditional loop repeated until an EXIT or RETURN statement. EXIT terminates the current or outer loop. CONTINUE skips to the next loop iteration. WHILE repeats a sequence while a boolean expression is true. FOR loops over a range of integer values, optionally in reverse order or with a step value.

Uploaded by

Aali Yah
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PL/PgSQL Tutorial 3

PL/PgSQL – Control Structures (Loops)


Most of the contents of this Labsheet is taken from the PostgreSQL8.5 Documentation. It is available at the
following link: http://developer.postgresql.org/pgdocs/postgres/plpgsql.html

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

CONTINUE [ label ] [ WHEN boolean-expression ];

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

The WHILE statement repeats a sequence of statements so long as the boolean-expression


evaluates to true. The expression is checked just before each entry to the loop body.
For example:

WHILE amount_owed > 0 AND gift_certificate_balance > 0 LOOP


-- some computations here
END LOOP;

WHILE NOT done LOOP


-- some computations here
END LOOP;

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:

FOR i IN 1..10 LOOP


-- i will take on the values 1,2,3,4,5,6,7,8,9,10 within the loop
END LOOP;

FOR i IN REVERSE 10..1 LOOP


-- i will take on the values 10,9,8,7,6,5,4,3,2,1 within the loop
END LOOP;

FOR i IN REVERSE 10..1 BY 2 LOOP


-- i will take on the values 10,8,6,4,2 within the loop
END LOOP;

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

You might also like