FOR statements
- Last Updated: November 7, 2022
- 1 minute read
- DataDirect Connectors
- JDBC
- Autonomous REST Connector 6.0
- Documentation
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
FORstatement 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
SELECTstatement 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
SELECTlist 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
FORloop. - 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