The FOREACH function iterates over an Connect CDC (SQData) cursor and executes a statement body once for
each fetched row.
When FOREACH() is used with a cursor, Connect CDC (SQData) maps the input arguments to the cursor USING
record, opens the cursor, fetches rows until end-of-data, runs the
statement body for each row, and closes the cursor automatically.
Tip:
FOREACH() also supports iteration over HASH variables. This topic describes cursor usage.Category
Cursor Functions
Syntax
FOREACH(cursor_name [, value_1 [, value_2 ...]]) statement_body
Parameters and descriptions
| Parameter | Description |
|---|---|
cursor_name |
Name of a cursor created with CREATE RDBMS
CURSOR, CREATE VSAM CURSOR, or
CREATE IMS CURSOR. |
value_1, value_2, ... |
Optional values mapped in order to the fields of the record named in the cursor USING clause. |
statement_body |
The Connect CDC (SQData) statement executed once for each row fetched successfully. |
Usage
-
For cursor processing,
FOREACH()is a compact alternative to explicitOPEN(),FETCH(), andCLOSE()logic.For best results:- The values supplied in
FOREACH()should match the number and order of the fields in the cursorUSINGrecord. - The record named in the cursor
DESCRIBED BYclause should match the number, order, and compatible types of the columns returned by the cursor. - The statement body should read the
DESCRIBED BYfields populated by the current fetch.
- The values supplied in
- If the cursor reaches end-of-data, iteration ends normally. Any other open or fetch failure is treated as a runtime failure.
Example
CREATE RDBMS CURSOR AS SQDATA_RDBMS_CURSOR WITH
/+
SELECT ACCOUNT_ID, ACCOUNT_STATUS
FROM SQDATA_LOOKUP_SOURCE
WHERE ACCOUNT_ID = ? AND REGION_CODE = ?
+/
DESCRIBED BY SQDATA_CURSOR_ROW
USING SQDATA_CURSOR_INPUT
;
FOREACH(SQDATA_RDBMS_CURSOR, SQDATA_CURSOR_INPUT.ACCOUNT_ID, SQDATA_CURSOR_INPUT.REGION_CODE)
DO
TARGET_ACCOUNT_ID = SQDATA_CURSOR_ROW.ACCOUNT_ID
TARGET_ACCOUNT_STATUS = SQDATA_CURSOR_ROW.ACCOUNT_STATUS
END