NonStop SQL/MP Reference Manual

Table Of Contents
NonStop SQL/MP Reference Manual142115
F-5
Examples—FETCH
Avoid using SQLCODE 100 as an end-of-file indicator. SQL resets SQLCODE to 0
when you close the associated cursor. Instead, define your own end-of-file flag.
Examples—FETCH
In the following example, assume you have a cursor that returns information from
the PARTS table. The host variables are declared in a Declare Section, and the
cursor declaration lists the columns to be retrieved. The FETCH statement lists a
corresponding host variable to receive the values returned for each column. (The
example uses the SQL statement terminator for COBOL85 programs.)
Variable declarations:
...
EXEC SQL BEGIN DECLARE SECTION END-EXEC.
Declare host variables:
01 HVAR1
02 HVAR2
03 HVAR3
EXEC SQL END DECLARE SECTION END-EXEC.
...
Main code:
EXEC SQL DECLARE CURSOR1 CURSOR FOR
SELECT COL1,
COL2,
COL3
FROM =PARTS
WHERE COL1 >= :HOSTVAR1
ORDER BY COL1
BROWSE ACCESS
END-EXEC.
...
EXEC SQL OPEN CURSOR1 END-EXEC.
EXEC SQL FETCH CURSOR1
INTO :HVAR1,
:HVAR2,
:HVAR3
END-EXEC.