The FOR statement executes the specified SELECT statement and then iterates the specified procedural statement against each row in the result set.

[<label> :] FOR <select_statement> DO <procedural_statement> END FOR [<label>]

where:

label
(optional) is the identifier for the FOR statement that can be referenced by other statements. If specified, the label at the beginning of the statement must match the label at the end.
select_statement
is the SELECT statement used to return the result set.
procedural_statement
is the procedural statement to be iterated against each row in the result set.
Note:
  • Column names in the SELECT list must not conflict with any other identifiers used as variables or parameters.
  • Columns named in the statement are read-only for the duration of the FOR loop.
  • The result list is built before the loop executes to prevent changes in the underlying data from changing the number of iterations in the loop.

For example:

CREATE PROCEDURE delete_duplicate_products()
MODIFIES SQL DATA
BEGIN ATOMIC
    DECLARE prev VARCHAR(32) DEFAULT '';
    FOR SELECT id,name FROM products DO
        IF product.name = prev THEN
            DELETE FROM products WHERE products.id = id;
        END IF;
        SET prev = name;
    END FOR;
END