DB2 - Banco de dados - EXECUTE - www.cadcobol.com.br
The EXECUTE statement executes a prepared SQL statement.
This statement can only be embedded in an application program. It is an executable statement that cannot be dynamically prepared.
For each global variable used as an expression in the USING clause or in the expression for an array-index, the privileges held by the authorization ID of the statement must include one of the following authorities:
For each global variable used as an assignment-target, the privileges held by the authorization ID of the statement must include one of the following authorities:
For statements where authorization checking is performed at statement execution time (DDL, GRANT, and REVOKE statements), the privileges held by the authorization ID of the statement must include those required to execute the SQL statement specified by the PREPARE statement. The authorization ID of the statement might be affected by the DYNAMICRULES bind option.
For statements where authorization checking is performed at statement preparation time (DML), no further authorization checking is performed on the SQL statement specified by the PREPARE statement.
For a dynamic CALL statement, parameter markers appearing in OUT and INOUT arguments to the procedure are output parameter markers. If any output parameter markers appear in the statement, the INTO clause must be specified (SQLSTATE 07007).
If the data type of an assignment-target is a row type, then there must be exactly one assignment-target specified (SQLSTATE 428HR), the number of columns must match the number of fields in the row type, and the data types of the columns of the fetched row must be assignable to the corresponding fields of the row type (SQLSTATE 42821).
If the data type of an assignment-target is an array element, then there must be exactly one assignment-target specified.
Before the EXECUTE statement is processed, the user must set the following fields in the input 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 or structured data type output data must be accommodated, there must be two SQLVAR entries for every output parameter marker.
SQLD must be set to a value greater than or equal to zero and less than or equal to SQLN.
For a dynamic CALL statement, parameter markers appearing in IN and INOUT arguments to the procedure are input parameter markers. For all other dynamic statements, all the parameter markers are input parameter markers. If any input parameter markers appear in the statement, the USING clause must be specified (SQLSTATE 07004).
If LOB or structured data type input data must be accommodated, there must be two SQLVAR entries for every parameter marker.
Specifies the number of rows of source data. The values for the insert or merge operation are specified in the USING clause.
host-variable or integer-constant is assigned to an integral value k. If host-variable is specified, it must be of type integer or short and must not include an indicator variable. k must be in the range 2 to 32767. If FOR host-variable or integer-constant ROWS is not provided, the SQL will be executed with array of size 1.
Let V denote an input variable or expression that corresponds to parameter marker P. The value of V is assigned to the target variable for P in accordance with the rules for assigning a value to a column. Thus:
When the prepared statement is executed, the value used in place of P is the value of the target variable for P or the result of the target expression 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.
Both input and output parameters should be specified in both the USING and INTO clause for an anonymous block (dynamic compound sql).
Let V denote an output assignment target that corresponds to parameter marker P, which is used for argument A of a procedure. The value of A is assigned to V in accordance with the rules for retrieving a value from a column. Thus:
When an SQL statement is executed or prepared, the package information relevant to the application issuing the request is loaded from the system catalog into the package cache. The actual executable section for the individual SQL statement is also placed into the cache: static SQL sections are read in from the system catalog and placed in the package cache when the statement is first referenced; dynamic SQL sections are placed directly in the cache after they have been created. Dynamic SQL sections can be created by an explicit statement, such as PREPARE or EXECUTE IMMEDIATE. Once created, sections for dynamic SQL statements may be recreated by an implicit prepare of the statement by the system if the original section has been deleted for space management reasons, or has become invalid due to changes in the environment.
Each SQL statement is cached at the database level and can be shared among applications. Static SQL statements are shared among applications using the same package; dynamic SQL statements are shared among applications using the same compilation environment, and the exact same statement text. The text of each SQL statement issued by an application is cached locally within the application for use if an implicit prepare is required. Each PREPARE statement in the application program can cache one statement. All EXECUTE IMMEDIATE statements in an application program share the same space, and only one cached statement exists for all these EXECUTE IMMEDIATE statements at a time. If the same PREPARE or any EXECUTE IMMEDIATE statement is issued multiple times with a different SQL statement each time, only the last statement will be cached for reuse. The optimal use of the cache is to issue a number of different PREPARE statements once at the start of the application, and then to issue an EXECUTE or OPEN statement as required.
When dynamic SQL statements are cached, a statement can be reused over multiple units of work without needing to prepare the statement again, unless the SQL statements prepared in a package are bound with the KEEPDYNAMIC NO option. The system recompiles the statement if necessary when environment changes occur.
The following events are examples of environment or data object changes that can cause cached dynamic statements to be implicitly prepared on the next PREPARE, EXECUTE, EXECUTE IMMEDIATE, or OPEN request:
The following list outlines the behavior that can be expected from cached dynamic SQL statements:
If an error occurs during an implicit prepare, an error will be returned for the request causing the implicit prepare (SQLSTATE 56098).
Example 1: In this C example, an INSERT statement with parameter markers is prepared and executed. Host variables h1 - h4 correspond to the format of TDEPT.
strcpy (s,"INSERT INTO TDEPT VALUES(?,?,?,?)"); EXEC SQL PREPARE DEPT_INSERT FROM :s; . . (Check for successful execution and put values into :h1, :h2, :h3, :h4) . . EXEC SQL EXECUTE DEPT_INSERT USING :h1, :h2, :h3, :h4;
Example 2: This EXECUTE statement uses an SQLDA.
EXECUTE S3 USING DESCRIPTOR :sqlda3
Example 3: Given a procedure to award an employee a bonus:
CREATE PROCEDURE GIVE_BONUS (IN EMPNO INTEGER, IN DEPTNO INTEGER, OUT CHEQUE INTEGER, INOUT BONUS DEC(6,0)) ...
strcpy (s, "CALL GIVE_BONUS(?, ?, ?, ?)"); EXEC SQL PREPARE DO_BONUS FROM :s; . . /* Check for successful execution and put values into :employee, :dept, and :bonus */ . . EXEC SQL EXECUTE DO_BONUS INTO :cheque_no, :bonus USING :employee, :dept, :bonus; . . /* Check for successful execution and process the values returned in :cheque_no and :bonus */