DB2 Version 9.7 for Linux, UNIX, and Windows

Declaring and using cursors in statically executed SQL applications

About this task

The DECLARE CURSOR statement must be used to declare a cursor, specify a name for the cursor, and the query that defines the result set associated with the cursor. The name specified is referred to in subsequent OPEN, FETCH, and CLOSE statements. The query is any valid select statement.

The declaration of a cursor can take place anywhere within the embedded SQL application, however it must be used before any references to it in other SQL statements.

Use the DECLARE statement to define the cursor. The following table provides examples for the supported host languages:
Table 1. Cursor Declarations by Host Language
Language Example Source Code
C and C++
EXEC SQL DECLARE C1 CURSOR FOR 
  SELECT PNAME, DEPT FROM STAFF 
  WHERE JOB=:host_var;
COBOL
EXEC SQL DECLARE C1 CURSOR FOR 
  SELECT NAME, DEPT FROM STAFF 
    WHERE JOB=:host-var END-EXEC.
FORTRAN
 EXEC SQL DECLARE C1 CURSOR FOR
+  SELECT NAME, DEPT FROM STAFF 
+  WHERE JOB=:host_var