FOREACH
Use the FOREACH statement to declare a direct cursor that can select and manipulate more than one row from a the result set of a query, or more than one element from a collection.
Direct sequential cursors that the FOREACH statement of SPL can create are distinct from the dynamic cursors that the DECLARE statement of SQL can create in SPL routines. (For the syntax and usage of dynamic cursors in SPL routines, see Declaring a Dynamic Cursor in an SPL Routine.)
SyntaxElement | Description | Restrictions | Syntax |
---|---|---|---|
cursor | Identifier that you declare here as the name of this direct cursor | Must be unique among names of cursors, prepared statements, and SPL variables in the routine | Identifier |
data_var | SPL variable in the calling routine that receives the returned values | Data type of data_var must be appropriate for returned value | Identifier |
function, procedure | SPL function or procedure to execute | Function or procedure must exist | Database Object Name |
SPL_var | SPL variable that contains the name of a routine to execute | Must be of type CHAR, VARCHAR, NCHAR, or NVARCHAR | Identifier |
Usage
- It declares and implicitly opens a direct sequential cursor.
- It obtains the first row from the query contained within the FOREACH loop, or else the first set of values from the called routine.
- It assigns to each variable in the variable list the value of the corresponding value from the active set that the SELECT statement or the called routine creates.
- It executes the statement block.
- It fetches the next row from the SELECT statement or called routine on each iteration, and it repeats steps 3 and 4.
- It terminates the loop when it finds no more rows that satisfy the SELECT statement or called routine. It closes the direct sequential cursor when the loop terminates.
Because the statement block can contain additional FOREACH statements, cursors can be nested. No limit exists on the number of nested cursors.
An SPL routine that returns more than one row, collection element, or set of values is called a cursor function. An SPL routine that returns only one row or value is called a noncursor function.
CREATE PROCEDURE foreach_ex() DEFINE i, j INT; FOREACH SELECT c1 INTO i FROM tab ORDER BY 1 INSERT INTO tab2 VALUES (i); END FOREACH FOREACH cur1 FOR SELECT c2, c3 INTO i, j FROM tab IF j > 100 THEN DELETE FROM tab WHERE CURRENT OF cur1; CONTINUE FOREACH; END IF UPDATE tab SET c2 = c2 + 10 WHERE CURRENT OF cur1; END FOREACH FOREACH EXECUTE PROCEDURE bar(10,20) INTO i INSERT INTO tab2 VALUES (i); END FOREACH END PROCEDURE; -- foreach_ex
- The cursor returns no further rows.
- The cursor is a Select cursor without a HOLD specification, and a transaction completes using the COMMIT or ROLLBACK statement.
- An EXIT statement executes, which transfers control out of the FOREACH statement.
- An exception occurs that is not trapped inside the body of the FOREACH statement. (See ON EXCEPTION.)
- A cursor in the calling routine that is executing this cursor routine (within a FOREACH loop) closes for any reason.