DB2 10.5 for Linux, UNIX, and Windows

fetch-first-clause

The fetch-first-clause sets a maximum number of rows that can be retrieved.

Read syntax diagramSkip visual syntax diagram
                .-1-------.                   
>>-FETCH FIRST--+---------+--+-ROW--+--ONLY--------------------><
                '-integer-'  '-ROWS-'         

The fetch-first-clause lets the database manager know that the application does not want to retrieve more than integer rows, regardless of how many rows there might be in the result table when this clause is not specified. An attempt to fetch beyond integer rows is handled the same way as normal end of data (SQLSTATE 02000). The value of integer must be a positive integer (not zero).

Use of the fetch-first-clause influences query optimization of the subselect or fullselect, based on the fact that at most integer rows will be retrieved. If both the fetch-first-clause is specified in the outermost fullselect and the optimize-for-clause is specified for the select statement, the database manager will use the integer from the optimize-for-clause to influence query optimization of the outermost fullselect.

Limiting the result table to the first integer rows can improve performance. The database manager will cease processing the query when it has determined the first integer rows. If both the fetch-first-clause and the optimize-for-clause are specified, the lower of the integer values from these clauses is used to influence the communications buffer size.

If the fullselect contains an SQL data change statement in the FROM clause, all the rows are modified regardless of the limit on the number of rows to fetch.