SQL/MP Programming Manual for COBOL

Data Retrieval and Modification
HP NonStop SQL/MP Programming Manual for COBOL529758-003
4-12
Using SQL Cursors
Using SQL Cursors
An SQL cursor is a named pointer that a host language program (C, COBOL, Pascal,
or TAL) can use to access a set of rows in a table or view, one row at a time. Using a
cursor, a program can process rows in the same way it might process records in a
sequential file. The program can test the data in each row at the current cursor position
and then, if the data meets certain criteria, the program can display, update, delete, or
ignore the row.
Figure 4-1 shows the steps you follow to declare and use a static SQL cursor in a
COBOL program. A cursor operation must execute each statement in this specified
order. All steps are required, even if you execute the FETCH statement only once to
retrieve a single row.
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