SQL/MP Programming Manual for COBOL85
Data Retrieval and Modification
HP NonStop SQL/MP Programming Manual for COBOL85—429326-004
4-12
Steps for Using a Cursor
Steps for Using a Cursor
These steps are shown in Figure 4-1. Each step is described in detail on subsequent
pages in this section.
1. Declare any host variables you plan to use with the cursor.
2. Name and define the cursor by using a DECLARE CURSOR statement. Follow the
conventions for an SQL identifier for the cursor name. The DECLARE CURSOR
statement also associates the cursor with a SELECT statement that specifies the
rows to retrieve.
3. Initialize any host variables you specified in the WHERE clause of the SELECT
statement in the DECLARE CURSOR statement.
Figure 4-1. Using a Static SQL Cursor in a COBOL Program
DATA DIVISION.
• • •
EXEC SQL BEGIN DECLARE SECTION END-EXEC.
* Declare host variable(s).
• • •
EXEC SQL END DECLARE SECTION END-EXEC.
• • •
EXEC SQL DECLARE CURSOR1 CURSOR FOR
SELECT COLUMN1, COLUMN2, COLUMN n
FROM =TABLE
WHERE COLUMN1 = :HOSTVAR-FIND-ROW
END-EXEC.
• • •
PROCEDURE DIVISION.
• • •
* Initialize the host variable(s).
MOVE INITIAL-VALUE TO HOSTVAR-FIND-ROW.
• • •
EXEC SQL OPEN CURSOR1 END-EXEC.
* Fetch data from a row into the host variable(s).
EXEC SQL FETCH CURSOR1
INTO :HOSTVAR_1, :HOSTVAR_2, :HOSTVAR n
END-EXEC.
* Process the row values in the host variable(s).
• • •
* Branch back to fetch another row.
• • •
EXEC SQL CLOSE CURSOR1 END-EXEC.
1
2
3
4
5
6
7
8
VST009.vsd