This example demonstrates the use of cursors. The particular cursor used here retrieves certain information from the Employees table in the sample database. The cursor is declared statically, meaning that the actual SQL statement to retrieve the information is hard coded into the source program. This is a good starting point for learning how cursors work. The Dynamic Cursor sample takes this first example and converts it to use dynamic SQL statements. See Dynamic cursor sample.
For information on where the source code can be found and how to build this example program, see Sample embedded SQL programs.
The open_cursor routine both declares a cursor for the specific SQL command and also opens the cursor.
Printing a page of information is accomplished by the print routine. It loops pagesize times, fetching a single row from the cursor and printing it out. Note that the fetch routine checks for warning conditions (such as
Row not found) and prints appropriate messages when they arise. In addition, the cursor is repositioned by this program to the row before the one that appears at the top of the current page of data.
The move, top, and bottom routines use the appropriate form of the FETCH statement to position the cursor. Note that this form of the FETCH statement doesn't actually get the data—it only positions the cursor. Also, a general relative positioning routine, move, has been implemented to move in either direction depending on the sign of the parameter.
When the user quits, the cursor is closed and the database connection is also released. The cursor is closed by a ROLLBACK WORK statement, and the connection is released by a DISCONNECT.