SQL/MP Programming Manual for COBOL

Dynamic SQL Operations
HP NonStop SQL/MP Programming Manual for COBOL529758-003
10-30
Performing the Database Request
Processing SELECT Statements
To determine if a statement is a SELECT statement, check SQL-STATEMENT-TYPE
OF SQLSA to see if it equals 1. If so, perform these steps:
1. Declare a cursor to handle the SELECT statement:
EXEC SQL DECLARE C1 CURSOR FOR S1 END-EXEC.
For more information about dynamic cursors, see Using Dynamic SQL Cursors,
following.
2. Begin a TMF transaction:
EXEC SQL BEGIN WORK END-EXEC.
(Depending on your transaction definition you might not want to do this for every
statement.)
3. Open the cursor:
EXEC SQL OPEN C1 USING DESCRIPTOR :IN-SQLDA END-EXEC.
4. Execute a loop to fetch the values and display them:
EXEC SQL FETCH C1 USING DESCRIPTOR :OUT-SQLDA END-EXEC.
* **SQLDA contains pointers to output data buffers
Display the values in a format according to data type. (For a repetitive display of
column names, use the output names buffer at this point and omit Steps 1through
3.)
If you know in advance which columns to select, you could use this form of the
FETCH statement:
EXEC SQL FETCH cursor INTO :
, :SAL END-EXEC.
* **Output parameters are :
and :SAL
5. Close the cursor:
EXEC SQL CLOSE C1 END-EXEC.
6. End the TMF transaction:
EXEC SQL COMMIT WORK END-EXEC.
(Depending on your transaction definition you might not want to do this for every
statement.)
Using Dynamic SQL Cursors
Dynamic SQL statements use cursors to process SELECT statements in the same way
static SQL statements use cursors. The program reads rows from a table, one by one,
and sends the column values to output data buffers specified in the program. This