Fetching data

Use a fetch statement to retrieve data through a cursor and assign it to host variables. The syntax for the fetch statement is:

exec sql [at connect_name] fetch [fetch orientation]cursor_name  
 into : host_variable 
 [[ indicator]: indicator_variable ]
 [,: host_variable 
 [[ indicator]: indicator_variable ]...]; 

where one host_variable exists for each column in the result rows.

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 fetch orientation specifies the fetch direction of the row to be fetched, if a cursor is scrollable. The options 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.