SQL/MX 2.x Reference Manual (H06.04+)

Embedded-Only SQL/MX Statements
HP NonStop SQL/MX Reference Manual540440-003
3-42
C Examples of FETCH
FETCH also returns an integer status code to SQLCODE, as follows:
SQLSTATE, the SQL:1999 standard, is the preferred status code for
NonStop SQL/MX.
C Examples of FETCH
Suppose that you have a cursor that returns information from the PARTS table.
The host variables are declared in a declaration section, and the cursor declaration
lists the columns to be retrieved. The FETCH statement lists host variables to
receive the values returned for each column:
/* Variable declarations */
long SQLCODE;
...
/* Host variable declarations */
EXEC SQL BEGIN DECLARE SECTION;
char SQLSTATE[6];
... hostvar;
... hostvar1;
... hostvar2;
... hostvar3;
EXEC SQL END DECLARE SECTION;
...
/* Declare cursor. */
EXEC SQL DECLARE cursor1 CURSOR FOR
SELECT COL1, COL2, COL3
FROM PARTS
WHERE COL1 >= :hostvar
ORDER BY COL1
READ UNCOMMITTED ACCESS;
...
/* Open cursor. */
EXEC SQL OPEN cursor1;
...
/* Fetch current row. */
EXEC SQL FETCH cursor1
INTO :hostvar1, :hostvar2, :hostvar3;
if SQLCODE = 100 goto ... ;
...
/* Close cursor. */
EXEC SQL CLOSE cursor1;
0 The FETCH was successful.
100 The result table is empty or the end of the table was encountered.
> 0 A warning was issued.
< 0 An error occurred.