Uses a fetch statement to retrieve data through a cursor and assign it to host variables.
exec sql [at connect_name] fetch [fetch orientation]cursor_name into : host_variable [[ indicator]: indicator_variable ] [,: host_variable [[ indicator]: indicator_variable ]...];
One host_variable exists for each column in the result rows.
Specifies the fetch direction of the row to be fetched, if a cursor is scrollable.
/*
** Fetch the first row in cursor resultset
*/
EXEC SQL FETCH FIRST FROM typelist INTO :a_type;
printf("\n%s\n", a_type);
/*
** Fetch the last row in cursor resultset
*/
EXEC SQL FETCH LAST FROM typelist INTO :a_type;
printf("\n%s\n", a_type);
When using host_variable, prefix each host variable with a colon, and separate it from the next host variable with a comma. The host variables listed in the fetch statement must correspond to Adaptive Server values that the select statement retrieves. Thus, the number of variables must match the number of returned values, they must be in the same order, and they must have compatible datatypes.
The options for fetch orienation are: NEXT, PRIOR, FIRST, LAST, ABSOLUTE fetch_offset and RELATIVE fetch_offset. If fetch orientation is not specified, next is default. If fetch orientation is specified, the cursor must be scrollable. The data that the fetch statement retrieves depends on the cursor position. The fetch statement typically retrieves single or multiple rows from the cursor result set, depending on the ROW_COUNT specification at cursor open time. If a cursor is not scrollable, fetch retrieves the next row in the result set. If a cursor is scrollable, commands in the fetch statement specify the row position to be fetched.
declare scrollable cursor