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.
The authorization rules are those defined for the SQL statement specified by EXECUTE. For example, see the description of INSERT
for the authorization rules that apply when an INSERT statement is executed using EXECUTE.
The authorization ID of the statement is the run-time authorization ID unless DYNUSRPRF(*OWNER) was specified on the CRTSQLxxx command when the program was created. For more information, see "Authorization IDs and Authorization-Names".
>>-EXECUTE--statement-name-------------------------------------->
>-----+------------------------------------+-------------------><
| .-,----------------. |
| V | |
+-USING-----host-variable---+--------+
'-USING DESCRIPTOR--descriptor-name--'
Before the EXECUTE statement is processed, the user must set the following fields in the SQLDA. (The rules for REXX are different. For more information, see the SQL Programming with Host Languages book.)
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*(80), where 80 is the length of an SQLVAR occurrence. If LOBs or distinct types are specified, there must be two SQLVAR entries for each parameter marker and SQLN must be set to two times the number of parameter markers.
SQLD must be set to a value greater than or equal to zero and less than or equal to SQLN. It must be the same as the number of parameter markers in the prepared statement. The nth variable described by the SQLDA corresponds to the nth parameter marker in the prepared statement. (For a description of an SQLDA, see Appendix C, SQL Descriptor Area (SQLDA).)
Note that RPG/400 does not provide the function for setting pointers. Because the SQLDA uses pointers to locate the appropriate host variables, you have to set these pointers outside your RPG/400 application.
Before the prepared statement is executed, each parameter marker in the statement is effectively replaced by its corresponding host variable. With the exception of the LIKE predicate, the replacement of a parameter marker is an assignment operation in which the source is the value of the host variable, and the target is a variable within the database manager. The attributes of the target variable are determined as follows:
If the parameter marker was specified as the pattern, the data type of the target is VARCHAR (if the first operand is CHAR or VARCHAR) or VARGRAPHIC (if the first operand is GRAPHIC or VARGRAPHIC). The length attribute of the target is the same as that of the first operand.
If the parameter marker was specified as the ESCAPE character, the length attribute of the target is 1.
| Note: | When the pattern specified in a LIKE predicate is a parameter marker, and a
fixed-length character host variable is used to replace the parameter
marker; specify a value for the host variable that is the correct
length. If you do not specify the correct length, the select will not
return the intended results.
For example, if the host variable is defined as CHAR(10), and the value WYSE% is assigned to that host variable, the host variable is padded with blanks on assignment. The pattern used is ('WYSE% '). This pattern requests the database manager to search for all values that start with 'WYSE' and end with ' '. If you intended to search for only the values that start with 'WYSE' you should assign the value 'WSYE%%%%%%' to the host variable. |
Let V denote a host variable 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:
However, unlike the rules for assigning a value to a column:
When the prepared statement is executed, 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.
This example of portions of a COBOL program shows how an INSERT statement with parameter markers is prepared and executed.
EXEC SQL BEGIN DECLARE SECTION END-EXEC.
77 EMP PIC X(6).
77 PRJ PIC X(6).
77 ACT PIC S9(4) COMP-4.
77 TIM PIC S9(3)V9(2).
01 HOLDER.
49 HOLDER-LENGTH PIC S9(4) COMP-4.
49 HOLDER-VALUE PIC X(80).
EXEC SQL END DECLARE SECTION END-EXEC.
.
.
.
MOVE 70 TO HOLDER-LENGTH.
MOVE "INSERT INTO EMP_ACT (EMPNO, PROJNO, ACTNO, EMPTIME)
- "VALUES (?, ?, ?, ?)" TO HOLDER.
EXEC SQL PREPARE MYINSERT FROM :HOLDER END-EXEC.
IF SQLCODE = 0
PERFORM DO-INSERT THRU END-DO-INSERT
ELSE
PERFORM ERROR-CONDITION.
DO-INSERT.
MOVE "000010" TO EMP.
MOVE "AD3100" TO PRJ.
MOVE 160 TO ACT.
MOVE .50 TO TIM.
EXEC SQL EXECUTE MYINSERT USING :EMP, :PRJ, :ACT, :TIM END-EXEC.
END-DO-INSERT.
.
.
.