Use FETCH FOR n ROWS

Applications that perform many FETCH statements in succession may be improved by using FETCH FOR n ROWS. With this clause, you can retrieve multiple rows of data from a table and put them into a host structure array or row storage area with a single FETCH.

An SQL application that uses a FETCH statement without the FOR n ROWS clause can be improved by using the multiple-row FETCH statement to retrieve multiple rows. After the host structure array or row storage area has been filled by the FETCH, the application can loop through the data in the array or storage area to process each of the individual rows. The statement runs faster because the SQL run-time was called only once and all the data was simultaneously returned to the application program.

You can change the application program to allow the database manager to block the rows that the SQL run-time retrieves from the tables.

In the following table, the program attempted to FETCH 100 rows into the application. Note the differences in the table for the number of calls to SQL run-time and the database manager when blocking can be performed.

Table 1. Number of Calls Using a FETCH Statement
  Database Manager Not Using Blocking Database Manager Using Blocking
Single-Row FETCH Statement 100 SQL calls 100 database calls 100 SQL calls 1 database call
Multiple-Row FETCH Statement 1 SQL run-time call 100 database calls 1 SQL run-time call 1 database call
Related information
FETCH statement