SQL/MP Programming Manual for COBOL

Data Retrieval and Modification
HP NonStop SQL/MP Programming Manual for COBOL529758-003
4-18
FETCH Statement
the DECLARE CURSOR, OPEN, INSERT, DELETE, UPDATE, and CLOSE statements
that refer to the cursor.
SQL/MP resets values in an SQLSA structure immediately before a FETCH statement
executes. If you use an SQLSA value elsewhere in your program, save the value in a
variable immediately after the FETCH statement executes. To monitor statistics for a
cursor, declare accumulator variables for the required values and add the SQLSA
values to the accumulator variables after each FETCH statement executes.
For audited tables and views, the FETCH statement must execute within the same
TMF transaction as the OPEN statement for the cursor.
This FETCH statement retrieves information from the PARTS table:
EXEC SQL BEGIN DECLARE SECTION END-EXEC.
01 PARTS.
02 PARTNUM PIC 9(4) DISPLAY.
02 PARTDESC PIC X(18).
02 PRICE PIC S9(16)V9(2) COMP.
02 QTY-AVAILABLE PIC S9(9) COMP.
...
EXEC SQL END DECLARE SECTION END-EXEC.
EXEC SQL DECLARE LIST-BY-PARTNUM CURSOR FOR
SELECT PARTNUM,
PARTDESC,
PRICE,
QTY-AVAILABLE
FROM =PARTS
WHERE PARTNUM >= :PARTNUM OF PARTS
ORDER BY PARTNUM
BROWSE ACCESS END-EXEC.
...
...
PROCEDURE DIVISION.
...
0100-GET-DATA.
EXEC SQL OPEN LIST-BY-PARTNUM END-EXEC.
PERFORM 0200-FETCH-ROWS WITH TEST AFTER UNTIL
SQLCODE OF SQLCA NOT = 0.
...
EXEC SQL CLOSE LIST-BY-PARTNUM END-EXEC.
...
0200-FETCH-ROWS.
EXEC SQL
FETCH LIST-BY-PARTNUM
INTO :PARTNUM OF PARTS,
:PARTDESC OF PARTS,
:PRICE OF PARTS,
:QTY-AVAILABLE OF PARTS
END-EXEC.