PL/pgSQL Loop Statements
Summary: in this tutorial, you will learn about the PL/pgSQL loop statement that executes a block of code repeatedly.
Introduction to PL/pgSQL Loop statement
The loop
defines an unconditional loop that executes a block of code repeatedly until terminated by an exit
or return
statement.
The following illustrates the syntax of the loop
statement:
Typically, you use an if
statement to terminate the loop based on a condition like this:
The exit
statement terminates the loop immediately.
It’s possible to place a loop statement inside another loop statement. When a loop
statement is placed inside another loop
statement, it is called a nested loop:
When you have nested loops, it’s necessary to use loop labels. The loop labels allow you to specify the loop in the exit
and continue
statements, indicating which loop these statements refer to.
PL/pgSQL loop statement examples
Let’s explore some examples of using the loop statement.
1) Basic PL/pgSQL loop example
The following example uses a loop
statement to display five numbers from 1 to five:
Output:
How it works.
First, declare a variable counter
and initialize its value to zero:
Second, increase the value of the counter
variable by one in each iteration of the loop:
Third, display the current value of the counter
:
Finally, terminate the loop if the current value of the counter
variable is 5:
Since the initial value of the counter
is zero, the loop executes five times before it is terminated.
In practice, you can combine the if
and exit
statements into a single statement like this:
For example:
Note that you will learn more about the exit statement in the upcoming tutorial.
2) Using a loop with a label
The following example illustrates how to use a loop label:
Output:
How it works.
In this example, we place a loop label my_loop
inside <<>>
before the LOOP
keyword.
Inside the loop’s body, we reference the loop label (my_loop
) in the exit
statement to explicitly instruct PostgreSQL to terminate the loop specified by the loop label: my_loop
.
It’ll be more practical to use a loop label when you have a nested loop.
3) Nested loop example
The following example illustrates how to use a nested loop with labels:
Output:
How it works.
First, declare two variables row_var
and col_var
, and initialize their values to zero:
Second, use the outer_loop
as the label for the outer loop. In the outer loop, increase the value of the row_var
by one, execute the nested loop, and reset the col_var
in each iteration.
If the row_var
is 3, exit the outer loop by referencing the outer_loop
label in the exit
statement.
Third, use the inner_loop
as the label for the inner loop. In the inner loop, increase the value of col_var
by one, display the current values of row_var
and col_var
variables, and terminate the inner loop when the value of col_var
is 3.
Summary
- Use the PL/pgSQL
LOOP
statement to create unconditional loops. - The loop can be nested.
- Use the
exit
statement to terminate a loop prematurely.