The FETCH statement assigns the values of the current row in a result table (see
result table name) to parameters.Syntax
<fetch_statement> ::=
FETCH [FIRST | LAST | NEXT | PREV | <position> | SAME] [<result_table_name>] INTO <parameter_spec>,...
<position> ::= POS (<unsigned_integer>) | POS (<parameter_spec>)
| ABSOLUTE <integer> | ABSOLUTE <parameter_spec>
| RELATIVE <integer> | RELATIVE <parameter_spec>
Explanation
If no result table name is specified, the fetch statement refers to the last unnamed result table that was generated (see
named/unnamed result table).Depending on the search method, either all the rows in the result table are searched when the
OPEN CURSOR statement, the SELECT statement (select_statement), or the SELECT statement (named_select_statement) is executed and the result table is generated, or each subsequent row in the result table is searched when a FETCH statement is executed, but they are not physically stored. This must be taken into account for the time behavior of FETCH statements. Depending on the isolation level selected, this can also cause locking problems with a FETCH, e.g. return code 500 - LOCK REQUEST TIMEOUT.Row not found
Let C be the position in the result table. The return code
100 - ROW NOT FOUND - is output and no values are assigned to the parameters if any of the following conditions is satisfied:FIRST | LAST | NEXT | PREV
Position: POS
Regardless of an
ORDER clause, there is an implicit order of the rows in a result table. This can be displayed by specifying a rowno column as a selected column. The specified position refers to this internal numbering.If a position is defined with POS, the parameter specification must denote a positive integer.
If a position that is less than or equal to the number of rows in the result table was defined with POS, C is set to the corresponding row and the values of this row are assigned to the parameters. If a position that is greater than the number of rows in the result table was specified, the message
100 – ROW NOT FOUND is output.Position: ABSOLUTE
Let x be the value of the integer or parameter specification specified with the position. Let
abs_x be the absolute value of x.Position: RELATIVE
Let x be the value of the integer or parameter specification specified with the position. Let
abs_x be the absolute value of x.FETCH SAME
The last row found in the result table is output again.
Parameter specification
The
parameter specification specified in position must denote an integer.The remaining parameters in the parameter specification are output parameters. The parameter identified by the nth parameter specification corresponds to the nth value in the current result table row. If the number of columns in this row exceeds the number of specified parameters, the column values for which no corresponding parameters exist are ignored. If the number of columns in the row is less than the number of specified parameters, no values are assigned to the remaining parameters. You must specify an
indicator name in order to assign NULL values or special NULL values.Numbers are converted and character strings are truncated or lengthened, if necessary, to suit the corresponding parameters. If an error occurs when assigning a value to a parameter, the value is not assigned and no further values are assigned to the corresponding parameters for this fetch statement. Any values that have already been assigned to parameters remain unchanged.
Let p be a parameter and v the corresponding value in the current row of the result table.
Further information
If no FOR REUSE was specified in the QUERY statement (see
SELECT statement), subsequent INSERT, update, or delete statements that refer to the underlying base table and are executed by the current user or by other users can cause several executions of a fetch statement to denote different rows in the result table, even though the same position was specified.You can prevent other users from making changes by executing a
LOCK statement for the entire table or by using the isolation level 2, 3, 15, 20, or 30 with the CONNECT statement or the LOCK option of the query statement.If a result table that was physically created contains
LONG columns and if the isolation levels 0, 1, and 15 are used, consistency between the content of the LONG columns and that of the other columns is not ensured. If the result table was not physically generated, consistency is not ensured at isolation level 0 only. For this reason, it is advisable to ensure consistency by using a LOCK statement or the isolation levels 2, 3, 20, or 30.