The OPEN statement opens a cursor so that it can be used to fetch rows from its result table.
Invocation
Although an interactive SQL facility might provide an interface that gives the appearance of interactive execution, this statement can only be embedded within an application program. It is an executable statement that cannot be dynamically prepared.
Authorization
See DECLARE CURSOR for the authorization required to use a cursor.
Syntax
>>-OPEN--cursor-name----+------------------------------------+->< | .-,----------------. | | V | | +-USING-----host-variable---+--------+ '-USING DESCRIPTOR--descriptor-name--'
Description
The DECLARE CURSOR statement must identify a SELECT statement, in one of the following ways:
The result table of the cursor is derived by evaluating that SELECT statement, using the current values of any host variables specified in it or in the USING clause of the OPEN statement. The rows of the result table may be derived during the execution of the OPEN statement and a temporary table may be created to hold them; or they may be derived during the execution of subsequent FETCH statements. In either case, the cursor is placed in the open state and positioned before the first row of its result table. If the table is empty the state of the cursor is effectively "after the last row".
Before the OPEN statement is processed, the user must set the following fields in the SQLDA:
The SQLDA must have enough storage to contain all SQLVAR occurrences. Therefore, the value in SQLDABC must be greater than or equal to 16 + SQLN*(N), where N is the length of an SQLVAR occurrence.
If LOB result columns need to be accommodated, there must be two SQLVAR entries for every select-list item (or column of the result table). See Effect of DESCRIBE on the SQLDA, which discusses SQLDOUBLED and LOB columns.
SQLD must be set to a value greater than or equal to zero and less than or equal to SQLN. For more information, see Appendix C, SQL Descriptor Area (SQLDA).
When the SELECT statement of the cursor is evaluated, the value used in place of P is the value of the target variable for P. For example, if V is CHAR(6), and the target is CHAR(8), the value used in place of P is the value of V padded with two blanks.
Notes
All cursors, except open cursors declared WITH HOLD, are in a closed state when a program issues a COMMIT statement.
A cursor can also be in the closed state because a CLOSE statement was executed or an error was detected that made the position of the cursor unpredictable.
Conversely, if a temporary table is not used, INSERT, UPDATE, and DELETE statements executed while the cursor is open can affect the result table if issued from the same unit of work. The Application Development Guide describes how locking can be used to control the effect of INSERT, UPDATE, and DELETE operations executed by concurrent units of work. Your result table can also be affected by operations executed by your own unit of work, and the effect of such operations is not always predictable. For example, if cursor C is positioned on a row of its result table defined as SELECT * FROM T, and a new row is inserted into T, the effect of that insert on the result table is not predictable because its rows are not ordered. Thus a subsequent FETCH C may or may not retrieve the new row of T.
Examples
Example 1: Write the embedded statements in a COBOL program that will:
EXEC SQL DECLARE C1 CURSOR FOR SELECT DEPTNO, DEPTNAME, MGRNO FROM DEPARTMENT WHERE ADMRDEPT = 'A00' END-EXEC. EXEC SQL OPEN C1 END-EXEC.
Example 2: Code an OPEN statement to associate a cursor DYN_CURSOR with a dynamically defined select-statement in a C program. Assuming two parameter markers are used in the predicate of the select-statement, two host variable references are supplied with the OPEN statement to pass integer and varchar(64) values between the application and the database. (The related host variable definitions, PREPARE statement, and DECLARE CURSOR statement are also shown in the example below.)
EXEC SQL BEGIN DECLARE SECTION; static short hv_int; char hv_vchar64[64]; char stmt1_str[200]; EXEC SQL END DECLARE SECTION; EXEC SQL PREPARE STMT1_NAME FROM :stmt1_str; EXEC SQL DECLARE DYN_CURSOR CURSOR FOR STMT1_NAME; EXEC SQL OPEN DYN_CURSOR USING :hv_int, :hv_vchar64;
Example 3: Code an OPEN statement as in example 2, but in this case the number and data types of the parameter markers in the WHERE clause are not known.
EXEC SQL BEGIN DECLARE SECTION; char stmt1_str[200]; EXEC SQL END DECLARE SECTION; EXEC SQL INCLUDE SQLDA; EXEC SQL PREPARE STMT1_NAME FROM :stmt1_str; EXEC SQL DECLARE DYN_CURSOR CURSOR FOR STMT1_NAME; EXEC SQL OPEN DYN_CURSOR USING DESCRIPTOR :sqlda;