Contents Index MESSAGE statement OUTPUT statement [Interactive SQL]

ASA SQL Reference
  SQL Statements

OPEN statement [ESQL] [SP]


Description 

Use this statement to open a previously declared cursor to access information from the database.

Syntax 

OPEN cursor-name
USING [ DESCRIPTOR sqlda-name | hostvar, ...] ]
WITH HOLD ]
ISOLATION LEVEL n ]
BLOCK n ]

cursor-name :  identifier or hostvar

sqlda-name :   identifier

Parameters 

Embedded SQL usage    After successful execution of the OPEN statement, the sqlerrd[3] field of the SQLCA (SQLIOESTIMATE) is filled in with an estimate of the number of input/output operations required to fetch all rows of the query. Also, the sqlerrd[2] field of the SQLCA (SQLCOUNT) is filled with either the actual number of rows in the cursor (a value greater than or equal to 0), or an estimate thereof (a negative number whose absolute value is the estimate). It will be the actual number of rows if the database server can compute it without counting the rows. The database can also be configured to always return the actual number of rows (see ROW_COUNTS option [database]), but this can be expensive.

If cursor-name is specified by an identifier or string, the corresponding DECLARE CURSOR must appear prior to the OPEN in the C program; if the cursor-name is specified by a host variable, the DECLARE CURSOR statement must execute before the OPEN statement.

USING DESCRIPTOR clause    The USING DESCRIPTOR clause is for Embedded SQL only. It specifies the host variables to be bound to the place-holder bind variables in the SELECT statement for which the cursor has been declared.

WITH HOLD clause    By default, all cursors are automatically closed at the end of the current transaction (COMMIT or ROLLBACK). The optional WITH HOLD clause keeps the cursor open for subsequent transactions. It will remain open until the end of the current connection or until an explicit CLOSE statement is executed. Cursors are automatically closed when a connection is terminated.

ISOLATION LEVEL clause    The ISOLATION LEVEL clause allows this cursor to be opened at an isolation level different from the current setting of the ISOLATION_LEVEL option. All operations on this cursor will be performed at the specified isolation level regardless of the option setting. If this clause is not specified, then the cursor's isolation level for the entire time the cursor is open is the value of the ISOLATION_LEVEL option when the cursor is opened. See How locking works.

The cursor is positioned before the first row (see Using cursors in embedded SQL or Using cursors in procedures and triggers).

BLOCK clause    This clause is for Embedded SQL use only. Rows are fetched by the client application in blocks (more than one at a time). By default, the number of rows in a block is determined dynamically based on the size of the rows and how long it takes the database server to fetch each row. The application can specify a maximum number of rows that should be contained in a block by specifying the BLOCK clause. For example, if you are fetching and displaying 5 rows at a time, use BLOCK 5. Specifying BLOCK 0 will cause one row at a time to be fetched, and also cause a FETCH RELATIVE 0 to always fetch the row again.

For more information, see FETCH statement [ESQL] [SP].

Usage 

The OPEN statement opens the named cursor. The cursor must be previously declared.

When the cursor is on a CALL statement, OPEN causes the procedure to execute until the first result set (SELECT statement with no INTO clause) is encountered. If the procedure completes and no result set is found, the SQLSTATE_PROCEDURE_COMPLETE warning is set.

Permissions 

Must have SELECT permission on all tables in a SELECT statement, or EXECUTE permission on the procedure in a CALL statement.

Side effects 

None.

See also 

DECLARE CURSOR statement [ESQL] [SP]

RESUME statement

PREPARE statement [ESQL]

FETCH statement [ESQL] [SP]

RESUME statement

CLOSE statement [ESQL] [SP]

Standards and compatibility 
Example 

The following examples show the use of OPEN in Embedded SQL.

EXEC SQL OPEN employee_cursor;

and

EXEC SQL PREPARE emp_stat FROM
'SELECT empnum, empname FROM employee WHERE name like ?';
EXEC SQL DECLARE employee_cursor CURSOR FOR emp_stat;
EXEC SQL OPEN employee_cursor USING :pattern;

The following example is from a procedure or trigger.

BEGIN
DECLARE cur_employee CURSOR FOR
   SELECT emp_lname
   FROM employee;
DECLARE name CHAR(40);
OPEN cur_employee;
LOOP
FETCH NEXT cur_employee into name;
    ...
END LOOP
CLOSE cur_employee;
END

Contents Index MESSAGE statement OUTPUT statement [Interactive SQL]