Lưu ý
Cần có ủy quyền mới truy nhập được vào trang này. Bạn có thể thử đăng nhập hoặc thay đổi thư mục.
Cần có ủy quyền mới truy nhập được vào trang này. Bạn có thể thử thay đổi thư mục.
Applies to:
Databricks SQL
Databricks Runtime 16.3 and above
Repeat the execution of a list of statements.
This statement may only be used within a compound statement.
Syntax
[ label : ] LOOP
{ stmt ; } [...]
END LOOP [ label ]
Parameters
-
An optional label for the loop, which is unique amongst all labels for statements within which the
LOOPstatement is contained. If an end label is specified, it must match the beginning label. The label can be used to LEAVE or ITERATE the loop. stmtA SQL statement
Examples
-- sum up all odd numbers from 1 through 10
> BEGIN
DECLARE sum INT DEFAULT 0;
DECLARE num INT DEFAULT 0;
sumNumbers: LOOP
SET num = num + 1;
IF num > 10 THEN
LEAVE sumNumbers;
END IF;
IF num % 2 = 0 THEN
ITERATE sumNumbers;
END IF;
SET sum = sum + num;
END LOOP 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
- WHILE Statement
- REPEAT Statement
- IF Statement
- ITERATE Statement
- LEAVE Statement