FOREACH Statement

Iterates over each row in a table or table expression, assigning the column values to the specified variables and executing the provided statements for each row.

The loop may be terminated early by using the BREAK statement. The current iteration of the loop can be abandoned and the next iteration of the loop started by using the CONTINUE statement.

Syntax


Parameters

Remarks

Examples

-- Iterate over a simple table with multiple columns
DROP TABLE IF EXISTS employees;
CREATE TEMP TABLE employees (id INTEGER, name TEXT, salary INTEGER);
INSERT INTO employees VALUES (1, 'Alice', 50000);
INSERT INTO employees VALUES (2, 'Bob', 60000);

FOREACH (@id, @name, @salary) IN employees BEGIN
PRINT CONCAT(@name, ' (ID: ', @id, ') earns $', @salary);
END