SQL/MP Programming Manual for COBOL

Dynamic SQL Operations
HP NonStop SQL/MP Programming Manual for COBOL529758-003
10-31
Performing the Database Request
subsection describes some guidelines for the use of cursors. The order for executing
statements for using a cursor with dynamic SQL operations is:
Follow these guidelines when you declare and use a cursor:
If you are using the HP COBOL or SQL compiler interface, you can use a host
variable wherever you can use the cursor-name and statement-name
parameters. For each new statement and cursor, store the name in the host
variable before executing the statements.
The DECLARE CURSOR, PREPARE, OPEN, FETCH, CLOSE, DELETE WHERE
CURRENT, UPDATE WHERE CURRENT, DESCRIBE INPUT, and DESCRIBE
statements for a particular cursor and its associated statement must all appear in
the same procedure, unless you are using a foreign cursor. See Using Foreign
Cursors on page 4-23.
The PREPARE statement does not have to precede the other statements in the
program listing order. However, the PREPARE statement must precede the
DECLARE CURSOR statement and any DESCRIBE, DESCRIBE INPUT, OPEN,
FETCH, and CLOSE statements (for extended dynamic SQL statements, where
the cursor and statement names are stored in host variables). Foreign cursors do
not have this restriction.
Using Cursors With a USING DESCRIPTOR Clause
If the program is handling input parameters with values entered at run time, use the
USING DESCRIPTOR clause with the OPEN statement to specify values for the
parameter values in the SELECT statement. The input SQLDA describes the input
location for each parameter. The DESCRIBE INPUT statement fills in the SQLVAR
entries in the SQLDA, and your program sets the VAR-PTR fields and prompts the user
for values for the parameters.
Operation Description
PREPARE statement-name
FROM :host-variable
Dynamically compiles the SELECT
statement defining the cursor
Issue the DESCRIBE INPUT and DESCRIBE
statements
DECLARE cursor-name CURSOR
FOR statement-name
Declares the cursor
OPEN cursor-name
USING DESCRIPTOR input-sqlda
Opens the cursor and gets parameter
values from an input data buffer in the
program
Loop until end-of-file
FETCH cursor-name
USING DESCRIPTOR output-sqlda
Retrieves data and outputs column values
to an output data buffer in the program
CLOSE cursor-name
Closes the cursor