Example: Declaring a cursor

The following example demonstrates declaring cursors:

exec sql declare C1 cursor for  
 select type, price from titles 
 where type like :WK-TYPE end-exec 

In this example, C1 is declared as a cursor for the rows that will be returned for the type and price columns. The precompiler generates no code for the declare cursor statement. It simply stores the select statement associated with the cursor.

When the cursor opens, the select statement or procedure in the declare cursor statement executes. When the data is fetched, the results are copied to the host variables.

NoteEach cursor’s open and declare statements must be in the same file. Host variables used within the declare statement must have the same scope as the one in which the open statement is defined. However, once the cursor is open, you can perform fetch and update/delete where current of on the cursor in any file.