Was this helpful?
FETCH
Valid in: ESQL, OpenAPI, ODBC, JDBC, .NET
The FETCH statement fetches data from a database cursor into host language variables.
The FETCH statement has the following formats:
Non-dynamic version:
EXEC SQL FETCH cursor_name
              INTO variable[:indicator_var] {, variable[:indicator_var]};
Dynamic version:
EXEC SQL FETCH cursor_name USING DESCRIPTOR descriptor_name;
cursor_name
Identifies an open cursor. Cursor_name can be either a string constant or a host language variable.
USING DESCRIPTOR descriptor_name
Identifies an SQLDA that contains type descriptions of one or more host language variables. Each element of the SQLDA is assigned the corresponding value in the current row of the cursor. For details, see the chapter “Working with Embedded SQL.”
The variables listed in the INTO clause or within the descriptor must be type-compatible with the values being retrieved. If a result expression is nullable, the host language variable that receives that value must have an associated null indicator.
If the statement does not fetch a row--a condition that occurs after all rows in the set have been processed--the sqlcode of the SQLCA is set to 100 (condition not found) and no values are assigned to the variables.
The statement must be terminated according to the rules of the host language.
The FETCH statement retrieves the results of the SELECT statement that is executed when a cursor is opened. When a cursor is opened, the cursor is positioned immediately before the first result row. The FETCH statement advances the cursor to the first (or next) row and loads the values in that row into the specified variables. Each FETCH statement advances the cursor one row.
There must be a one-to-one correspondence between variables specified in the INTO or USING clause of FETCH and expressions in the SELECT clause of the DECLARE CURSOR statement. If the number of variables does not match the number of expressions, the preprocessor generates a warning and, at runtime, the SQLCA variable sqlwarn3 is set to W.
The variables listed in the INTO clause can include structures that substitute for some or all of the variables. The structure is expanded by the preprocessor into the names of its individual variables; therefore, placing a structure name in the INTO clause is equivalent to enumerating all members of the structure in the order in which they were declared.
The variables listed in the INTO clause or within the descriptor must be type-compatible with the values being retrieved. If a result expression is nullable, the host language variable that receives that value must have an associated null indicator.
If the statement does not fetch a row—a condition that occurs after all rows in the set have been processed—the sqlcode of the SQLCA is set to 100 (condition not found) and no values are assigned to the variables.
The statement must be terminated according to the rules of the host language.
Last modified date: 01/30/2023