sequence of statements so long as the condition expression
evaluates to true. The condition is checked just before
each entry to the loop body.
<synopsis>
<synopsis>
<optional><<label>></optional>
<optional><<label>></optional>
WHILE <replaceable>expression</replaceable> LOOP
WHILE <replaceable>expression</replaceable> LOOP
<replaceable>statements</replaceable>
<replaceable>statements</replaceable>
END LOOP;
END LOOP;
</synopsis>
</synopsis>
The WHILE statement repeats a
sequence of statements so long as the condition expression
evaluates to true. The condition is checked just before
each entry to the loop body.
</para>
<para>
For example:
For example:
<programlisting>
<programlisting>
WHILE amount_owed > 0 AND gift_certificate_balance > 0 LOOP
WHILE amount_owed > 0 AND gift_certificate_balance > 0 LOOP
...
@@ -1288,25 +1307,22 @@ WHILE NOT boolean_expression LOOP
...
@@ -1288,25 +1307,22 @@ WHILE NOT boolean_expression LOOP
END LOOP;
END LOOP;
</programlisting>
</programlisting>
</para>
</para>
</listitem>
</sect3>
</varlistentry>
<varlistentry>
<sect3>
<term>
<title>FOR (integer for-loop)</title>
FOR
</term>
<listitem>
<para>
<para>
<synopsis>
<synopsis>
<optional><<label>></optional>
<optional><<label>></optional>
FOR <replaceable>name</replaceable> IN <optional> REVERSE </optional> <replaceable>expression</replaceable> .. <replaceable>expression</replaceable> LOOP
FOR <replaceable>name</replaceable> IN <optional> REVERSE </optional> <replaceable>expression</replaceable> .. <replaceable>expression</replaceable> LOOP
<replaceable>statements</replaceable>
<replaceable>statements</replaceable>
END LOOP;
END LOOP;
</synopsis>
</synopsis>
A loop that iterates over a range of integer values. The variable
<replaceable>name</replaceable> is automatically created as type
This form of FOR creates a loop that iterates over a range of integer
values. The variable
<replaceable>name</replaceable> is automatically defined as type
integer and exists only inside the loop. The two expressions giving
integer and exists only inside the loop. The two expressions giving
the lower and upper bound of the range are evaluated once when entering
the lower and upper bound of the range are evaluated once when entering
the loop. The iteration step is normally 1, but is -1 when REVERSE is
the loop. The iteration step is normally 1, but is -1 when REVERSE is
...
@@ -1327,18 +1343,16 @@ FOR i IN REVERSE 10..1 LOOP
...
@@ -1327,18 +1343,16 @@ FOR i IN REVERSE 10..1 LOOP
END LOOP;
END LOOP;
</programlisting>
</programlisting>
</para>
</para>
</listitem>
</varlistentry>
</variablelist>
</sect3>
</sect3>
</sect2>
<sect3 id="plpgsql-records-iterating">
<sect2 id="plpgsql-records-iterating">
<title>Iterating Through Records</title>
<title>Looping Through Query Results</title>
<para>
<para>
Using a different type of FOR loop, you can iterate through
Using a different type of FOR loop, you can iterate through
the results of a query and manipulate that data
the results of a query and manipulate that data
accordingly. The syntax is as follows:
accordingly. The syntax is:
<synopsis>
<synopsis>
<optional><<label>></optional>
<optional><<label>></optional>
FOR <replaceable>record | row</replaceable> IN <replaceable>select_query</replaceable> LOOP
FOR <replaceable>record | row</replaceable> IN <replaceable>select_query</replaceable> LOOP
...
@@ -1402,9 +1416,210 @@ END LOOP;
...
@@ -1402,9 +1416,210 @@ END LOOP;
declared as a record/row variable. If not, it'spresumedtobe
declared as a record/row variable. If not, it'spresumedtobe