Using The Multiple-Row FETCH Statement: Step 1:declare A Cursor
Using The Multiple-Row FETCH Statement: Step 1:declare A Cursor
The multiple-row FETCH statement can be used to retrieve multiple rows from a table or
view with a single FETCH. The program controls the blocking of rows by the number of
rows requested on the FETCH statement (OVRDBF has no effect). The maximum
number of rows that can be requested on a single fetch call is 32767. Once the data is
retrieved, the cursor is positioned on the last row retrieved.
The multiple-row FETCH statement can be used with both serial and scrollable cursors.
The operations used to define, open, and close a cursor for a multiple-row FETCH remain
the same. Only the FETCH statement changes to specify the number of rows to retrieve
and the storage where the rows are placed.
After each multiple-row FETCH, information is returned to the program through the
SQLCA. In addition to the SQLCODE and SQLSTATE fields, the SQLERRD provides
the following information:
Other Clauses
To move the contents of a selected row into your program's host variables, use the
FETCH statement. The SELECT statement within the DECLARE CURSOR statement
identifies rows that contain the column values your program wants. However, SQL does
not retrieve any data for your application program until the FETCH statement is issued.
The serial cursor FETCH statement looks like this:
EXEC SQL
FETCH cursor-name
INTO :host variable-1[, :host variable-2] ...
END-EXEC.
When your program has positioned the cursor on a row, you can update its data by using
the UPDATE statement with the WHERE CURRENT OF clause. The WHERE
CURRENT OF clause specifies a cursor that points to the row you want to update. The
UPDATE ... WHERE CURRENT OF statement looks like this:
EXEC SQL
UPDATE table-name
SET column-1 = value [, column-2 = value] ...
WHERE CURRENT OF cursor-name
END-EXEC.
Step 5b: Delete the current row
When your program has retrieved the current row, you can delete the row by using the
DELETE statement. To do this, you issue a DELETE statement designed for use with a
cursor; the WHERE CURRENT OF clause specifies a cursor that points to the row you
want to delete. The DELETE ... WHERE CURRENT OF statement looks like this:
EXEC SQL
DELETE FROM table-name
WHERE CURRENT OF cursor-name
END-EXEC.
Step 6: Close the Cursor
If you processed the rows of a result table for a serial cursor, and you want to use the
cursor again, issue a CLOSE statement to close the cursor prior to re-opening it.
EXEC SQL
CLOSE cursor-name
END-EXEC.
If you processed the rows of a result table and you do not want to use the cursor again,
you can let the system close the cursor. The system automatically closes the cursor when:
A COMMIT without HOLD statement is issued and the cursor is not declared
using the WITH HOLD clause.
Dynamic Cursor
To issue a dynamic SQL statement, you must use the statement with either an EXECUTE
statement or an EXECUTE IMMEDIATE statement, because dynamic SQL statements
are not prepared at precompile time and therefore must be prepared at run time. The
EXECUTE IMMEDIATE statement causes the SQL statement to be prepared and run
dynamically at program run time.
There are two basic types of dynamic SQL statements: SELECT statements and nonSELECT statements. Non-SELECT statements include such statements as DELETE,
INSERT, and UPDATE.
1. Run the SQL statement using EXECUTE IMMEDIATE, or PREPARE the SQL
statement, then EXECUTE the prepared statement.
2. Handle any SQL return codes that might result.
The following is an example of an application running a dynamic SQL non-SELECT
statement (stmtstrg):
EXEC SQL
EXECUTE IMMEDIATE: stmtstrg;
Using the PREPARE and EXECUTE statements
If non-SELECT statements contain no parameter markers, they can be run
dynamically using the EXECUTE IMMEDIATE statement. However, if the nonSELECT statements have parameter markers, they must be run using PREPARE
and EXECUTE.