FETCH statement

Description

Fetches single or multiple rows from the cursor result set, depending on the ROW_COUNT specification at CURSOR OPEN time.

If a cursor is specified as scrollable, the fetch orientation in the FETCH statement specifies the fetch direction.

If the cursor is not specified as scrollable, FETCH retrieves the next row in the result set.

Syntax

EXEC SQL DECLARE [ <fetch orientation> ]
         [ FROM ] <cursor name>
         { [ INTO <fetch target list> ] |
         [SQL DESCRIPTOR <>]
<fetch orientation> : : =
        | NEXT
        | PRIOR
        | FIRST
        | LAST
        | ABSOLUTE <fetch_offset>
        | RELATIVE <fetch_offset>
<fetch offset> : : =
        <signed_numeric_literal>
<fetch target list> : : =
        <target specification>
        [ { <comma> <target specification> } ]
END-EXEC

Parameters

fetch orientation

Specified as NEXT, PRIOR, FIRST, LAST, ABSOLUTE, or RELATIVE.

If fetch orientation is not specified, NEXT is the default.

NoteIf you specify fetch orientation as any type except NEXT on a non-scrollable cursor, you receive the following message:

The fetch type can only be used with scrollable cursors.

If fetch orientation positions the cursor beyond the last row or before the first row, sqlca.sqlcode is set to 100, indicating that no rows are found. If an error handler is installed, it may provide additional information.

fetch offset

Specified as an exact, signed numeric value with a scale of zero.