Database Reference
In-Depth Information
DECLARE
grade integer := 10;
BEGIN
WHILE grade <= 100
LOOP
grade := grade * subjects;
END LOOP;
RETURN grade;
END;
$$ LANGUAGE plpgsql;
The result can be seen using the following statement:
warehouse_db=# SELECT while_loop(5);
while_loop
------------
250
(1 row)
The FOR loop
Even if you are not familiar with the FOR loop, it is one of the most important loops
and part of the PL/pgSQL implementation of the LOOP structures.
The syntax of the FOR loop is as follows:
FOR i in 1...10
LOOP
Statements
END LOOP;
The FOR loop iterates over a range of integer values. An iterative integer is declared
here and doesn't need to be declared in the DECLARE block. The life scope of this
variable remains within the FOR loop and ends after the loop exits. By default, it
iterates with a step of 1, unless speciied in the BY clause. Iteration ranges in the
upper and lower ranges are deined as two expressions. If the REVERSE clause is
given, then the iterated value will not step up but will be subtracted. We will irst
implement a simple FOR loop using the same grade example, as follows:
warehouse_db=# CREATE OR REPLACE FUNCTION first_for_loop(subjects
integer)
RETURNS integer AS $$
DECLARE
grade integer := 2;
BEGIN
FOR i IN 1..10
 
Search WWH ::




Custom Search