SQL/MP Programming Manual for C
Data Retrieval and Modification
HP NonStop SQL/MP Programming Manual for C—429847-008
4-20
FETCH Statement
FETCH Statement
The FETCH statement positions the cursor at the next row of the result table and
transfers a value from each column in the row specified by the associated SELECT
statement to the corresponding host variable.
To run a FETCH statement, a process started by the program must have read access
to tables or views associated with the cursor. For information about process access,
see Required Access Authority on page 7-1.
NonStop SQL/MP returns these values to sqlcode after a FETCH statement.
The cursor must be open when the FETCH statement runs. The FETCH statement
must also run within the scope of all other SQL statements, including the DECLARE
CURSOR, OPEN, INSERT, DELETE, UPDATE, and CLOSE statements, that refer to
the cursor.
NonStop SQL/MP resets values in an SQLSA structure immediately before a FETCH
statement runs. If you use an SQLSA value elsewhere in your program, save the value
in a variable immediately after the FETCH statement runs. 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 runs.
For audited tables and views, the FETCH statement must run 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;
struct parts_type /* host variables */
{
short partnum;
char partdesc[19];
long price;
short qty_available
} parts_rec;
...
EXEC SQL END DECLARE SECTION;
...
...
EXEC SQL DECLARE list_by_partnum CURSOR FOR
SELECT partnum,partdesc,price,qty_available
FROM =parts
sqlcode Value Description
0 The FETCH statement was successful.
100 The end of a table was encountered.
<0 An error occurred;
sqlcode contains the error number.
>0 (¦100) A warning occurred;
sqlcode contains the first warning number.