7.6.6. WHILE … DO
Used for
Looping constructs
Available in
PSQL
Syntax
WHILE <condition> DO
<compound_statement>
Argument | Description |
---|---|
condition | A logical condition returning TRUE, FALSE or UNKNOWN |
single_statement | A single statement terminated with a semicolon |
compound_statement | Two or more statements wrapped in |
A WHILE
statement implements the looping construct in PSQL. The statement or the block of statements will be executed until the condition returns TRUE. Loops can be nested to any depth.
Example
A procedure calculating the sum of numbers from 1 to I shows how the looping construct is used.
CREATE PROCEDURE SUM_INT (I INTEGER)
RETURNS (S INTEGER)
AS
BEGIN
s = 0;
WHILE (i > 0) DO
BEGIN
s = s + i;
i = i - 1;
END
END
Executing the procedure in isql:
EXECUTE PROCEDURE SUM_INT(4);
the result is:
S
==========
10
See also
IF … THEN … ELSE
, LEAVE
, EXIT
, FOR SELECT
, FOR EXECUTE STATEMENT