Retrieve Results Using Cursors¶
To give your program the ability to access the database or issue other database statements while processing rows retrieved as the result of the select, a cursor must be used to retrieve those rows.
To use cursors, after the SQLDA has been analyzed and result variables have been allocated and pointed at, the program must declare and open a cursor to fetch the result rows.
The syntax of the cursor declaration for a dynamically defined SELECT statement is:
This statement associates the SELECT statement represented by statement_name with the specified cursor. Statement_name is the name assigned to the statement when the statement was prepared. As with non-dynamic cursor declarations, the SELECT statement is not evaluated until the cursor is opened. After opening the cursor, the program retrieves the result rows using the FETCH statement with the SQLDA instead of the list of output variables.
The syntax for a cursor FETCH statement is:
Before the FETCH statement, the program has filled the result descriptor with the addresses of the result storage areas. When executing the FETCH statement, the result columns are copied into the result areas referenced by the descriptor.
The following example elaborates on an earlier example in this section. The program reads a statement from the terminal. If the statement is βquitβ the program ends; otherwise, the program prepares the statement. If the statement is not a select, it is executed. If the statement is a FETCH statement, it is described, a cursor is opened, and the result rows are fetched. Error handling is not shown.