Host Programs, FETCH (Part 1 of 2)
If the cursor is defined as follows:
EXEC SQL
DECLARE VENDOR_CSR CURSOR FOR
SELECT VENDOR_NAME, STATUS
FROM VENDOR_TBL
WHERE COUNTRY = 'USA'
END-EXEC
To bring a row of the result table into your host program for processing, you must use a FETCH statement. The syntax is:
EXEC SQL
FETCH cursor_name
INTO :host_variable1[:host_ind1],
:host_variable2[:host_ind2]...
END-EXEC
And the statement is:
EXEC SQL
FETCH VENDOR_CSR
INTO :VEN-NAME,
:VEN-STATUS:VEN-STATUS-IND
END-EXEC
The INTO host variable name can be one name to hold all the columns retrieved (like a record) or it can be many names, one for each column retrieved. Null indicators should be used if columns can be null (such as STATUS).
Each time the FETCH statement is executed, it is like retrieving a single record from a sequential file. You can not go backwards to a previously retrieved row without closing and reopening the cursor. You also can not skip rows. If the FETCH statement retrieves a row, SQLCODE will be set to +0. If the FETCH statement fails to retrieve a row because there are no more rows to retrieve, SQLCODE will be set to +100. See SQLCODE and SQLSTATE.