REPEAT statement
Applies to: Databricks SQL
Databricks Runtime 16.3 and above
Preview
This feature is in Public Preview.
Repeat the execution of a list of statements until a condition is true.
This statement may only be used within a compound statement.
Syntax
[ label : ] REPEAT
{ stmt ; } [...]
UNTIL cond
END REPEAT [ label ]
Parameters
-
An optional label for the loop, which is unique amongst all labels for statements within which the
REPEATstatement is contained. The label can be used to LEAVE or ITERATE the loop. -
condAny expression evaluating to a BOOLEAN
-
stmtA SQL statement
Examples
SQL
-- sum up all odd numbers from 1 through 10
> BEGIN
DECLARE sum INT DEFAULT 0;
DECLARE num INT DEFAULT 0;
sumNumbers: REPEAT
SET num = num + 1;
IF num % 2 = 0 THEN
ITERATE sumNumbers;
END IF;
SET sum = sum + num;
UNTIL num = 10
END REPEAT 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
Related articles
- SQL Scripting
- CASE Statement
- Compound Statement
- FOR Statement
- IF Statement
- ITERATE Statement
- WHILE Statement
- LEAVE Statement
- LOOP Statement