layout: global title: WHILE statement displayTitle: WHILE statement license: | Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Repeat the execution of a list of statements while a condition is true.
This statement may only be used within a compound statement.
[ label : ] WHILE cond DO
{ stmt ; } [...]
END WHILE [ label ]
label
An optional label for the loop, which is unique amongst all labels for statements within which the WHILE statement is contained. The label can be used to LEAVE or ITERATE the loop.
cond
Any expression evaluating to a BOOLEAN.
stmt
A SQL statement.
-- sum up all odd numbers from 1 through 10 > BEGIN DECLARE sum INT DEFAULT 0; DECLARE num INT DEFAULT 0; sumNumbers: WHILE num < 10 DO SET num = num + 1; IF num % 2 = 0 THEN ITERATE sumNumbers; END IF; SET sum = sum + num; END WHILE sumNumbers; VALUES (sum); END; 25 -- Compare with the much more efficient relational computation: > SELECT sum(num) FROM range(1, 10) AS t(num) WHERE num % 2 = 1; 25