SQL/MP Reference Manual
HP NonStop SQL/MP Reference Manual—523352-013
F-5
Examples—FETCH
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.
EXEC SQL CLOSE CURSOR1 END-EXEC.
These steps demonstrate a dynamic SQL FETCH. The code uses FETCH with
USING DESCRIPTOR to return information on SELECT columns of which there is
no previous knowledge. (The example uses the SQL statement terminator for C,
Pascal, and TAL programs.)
1. Declare an SQLDA to hold input parameters and name the area SDAI;
NAMESINPUT is the names buffer. The values 5 and 39 are arbitrary values
chosen for the size of the SQLDA and the names buffer. Your program can use
different values or allocate memory dynamically.
EXEC SQL INCLUDE SQLDA (SDAI, 5, NAMESINPUT, 39);
2. Declare an SQLDA to hold output variables (SELECT columns) and name it
SDAO; NAMESOUTPUT is the names buffer:
EXEC SQL INCLUDE SQLDA (SDAO, 5, NAMESOUTPUT, 39);
3. Read an SQL statement input from the terminal and store it in variable H1.
4. Prepare the input as S1:
EXEC SQL PREPARE S1 FROM :H1;