Declaring a Cursor

Essentially, a cursor is an SQL statement, introduced with a DECLARE statement in the Procedure Division or the Data Division. The cursor does not start the query; the cursor simply identifies the query in the program.

Syntax

DECLARE cursorname CURSOR FOR SELECT_statement 

where cursorname is the name you assign to the cursor and SELECT_statement is a SELECT statement that returns multiple rows.

Example

The program select3.sqb contains the following code:

EXEC SQL
   DECLARE COBCUR1 CURSOR FOR
       SELECT
            C_NUMBER, C_FIRST_NAME, C_LAST_NAME, 
            TO_CHAR, C_INFO
       FROM CUSTOMER
       WHERE C_NUMBER >= :C_NUMBER
END-EXEC.

Here COBCUR1 is the name of the cursor. The operator in the WHERE clause indicates the possibility that more than one row will be returned.