SQL/MP Programming Manual for COBOL

Data Retrieval and Modification
HP NonStop SQL/MP Programming Manual for COBOL529758-003
4-16
OPEN Statement
CLOSE statements, that refer to the cursor. The DECLARE CURSOR statement
must also be within the scope of the statements that refer to the cursor.
The DECLARE CURSOR statement does not affect the values in the SQLCA and
SQLSA data structures.
Example 4-2 declares a cursor named LIST-BY-PARTNUM:
OPEN Statement
The OPEN statement opens an SQL cursor. The OPEN operation orders and defines
the set of rows in the result table and then positions the cursor before the first row.
The OPEN statement does not acquire any locks unless a sort is necessary to order
the selected rows. (The FETCH statement acquires any locks associated with a
cursor.)
To execute an OPEN statement for a cursor, a process started by the program must
have the access authority described in Access Requirements for Cursors on
page 4-13.
If the associated SELECT statement contains host variables in the WHERE clause,
you must initialize these host variables before you execute the OPEN statement. When
the OPEN statement executes, SQL/MP defines the set of rows in the result table and
places the input host variables in its buffers. If you do not initialize the host variables
before you execute the OPEN statement, these problems can occur:
If a host variable contains values with unexpected data types, overflow or
truncation errors can occur.
Example 4-2. Declaring a Cursor
EXEC SQL BEGIN DECLARE SECTION END-EXEC.
01 PARTS.
02 PARTNUM PIC 9(4) DISPLAY.
02 PARTDESC PIC X(18).
02 PRICE PIC S9(16)V9(2) COMP.
02 QTY-AVAILABLE PIC S9(9) COMP.
...
EXEC SQL END DECLARE SECTION END-EXEC.
...
EXEC SQL DECLARE LIST-BY-PARTNUM CURSOR FOR
SELECT PARTNUM,
PARTDESC,
PRICE,
QTY-AVAILABLE
FROM =PARTS
WHERE PARTNUM>= :PARTNUM OF PARTS
ORDER BY PARTNUM
BROWSE ACCESS;
PROCEDURE DIVISION.
...