DB2 - Banco de dados - OPEN CURSOR - www.cadcobol.com.br
The OPEN statement opens a cursor so that it can be used to fetch rows from its result table.
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. When invoked using the command line processor, some options cannot be specified.
If a global variable is referenced, the privileges held by the authorization ID of the statement must include one of the following authorities:
Group privileges are not considered because this statement cannot be dynamically prepared.
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 the SELECT statement. The evaluation uses the current values of any special registers, global variables, or PREVIOUS VALUE expressions specified in the SELECT statement, and the current values of any host variables specified in the SELECT statement or 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.
after the last row
Names a cursor variable. The value of the cursor variable must not be null (SQLSTATE 34000). A cursor variable that is directly or indirectly assigned a cursor value constructor can be used only in an OPEN statement that is in the same scope as the assignment (SQLSTATE 51044). If the cursor value constructor assigned to the cursor variable specified a statement-name, the OPEN statement must be in the same scope where that statement-name was explicitly or implicitly declared (SQLSTATE 51044).
When the OPEN statement is executed, the underlying cursor of the cursor variable must be in the closed state. The result table of the underlying cursor is derived by evaluating the SELECT statement or dynamic statement associated with the cursor variable. The evaluation uses the current values of any special registers, global variables, or PREVIOUS VALUE expressions specified in the SELECT statement, and the current values of any variables specified in the SELECT statement or 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.
An OPEN statement using a cursor-variable-name can only be used within a compound SQL (compiled) statement.
Introduces the values that are substituted for the parameter markers or variables in the statement of the cursor. For an explanation of parameter markers, see PREPARE.
PREPARE
If a statement-name is specified in the DECLARE CURSOR statement or the cursor value constructor associated with the cursor variable that includes parameter markers, USING must be used. If the prepared statement does not include parameter markers, USING is ignored.
If a select-statement is specified in the DECLARE CURSOR statement or the non-parameterized cursor value constructor associated with the cursor variable, USING may be used to override the variable values.
Identifies a variable or a host structure declared in the program in accordance with the rules for declaring variables and host variables. The number of variables must be the same as the number of parameter markers in the prepared statement. The nth variable corresponds to the nth parameter marker in the prepared statement. Where appropriate, locator variables and file reference variables can be provided as the source of values for parameter markers.
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.
Explicit rollback of an OPEN statement, or rollback to a savepoint before an OPEN statement, closes the cursor. If the cursor definition contains a data change statement within the FROM clause of a fullselect, the results of the data change statement are rolled back.
Changes to rows in a table that is targeted by a data change statement nested within a SELECT statement or a SELECT INTO statement are processed when the cursor opens, and are not undone if an error occurs during a fetch operation against that cursor.
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.
The underlying cursor of a cursor variable is closed if the cursor variable goes out of scope and there are no other cursor variables that referenced that underlying cursor.
Conversely, if a temporary buffer 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, and any NEXT VALUE expressions in the SELECT statement are evaluated as each row is fetched. This result table can also be affected by operations executed by the same 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.
EXEC SQL DECLARE C1 CURSOR FOR SELECT DEPTNO , DEPTNAME , MGRNO FROM DEPARTMENT WHERE ADMRDEPT = 'A00' END-EXEC. EXEC SQL OPEN C1 END-EXEC.
EXEC SQL BEGIN DECLARE SECTION; static short hv_int; char hv_vchar64[65]; 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;
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;
CREATE PROCEDURE PROC1 (OUT P1 CURSOR)LANGUAGE SQL BEGIN SET P1=CURSOR FOR SELECT DEPTNO , DEPTNAME , MGRNO FROM DEPARTMENT WHERE ADMRDEPT='A00'; -- OPEN P1; -- END;