Executes a dynamic SQL statement from a prepared statement.
See execute immediate.
exec sql [at connection_name] execute statement_name [into {host_var_list | descriptor descriptor_name | sql descriptor descriptor_name}] [using {host_var_list | descriptor descriptor_name | sql descriptor descriptor_name}] end-exec
Do not confuse the Embedded SQL execute statement
with the Embedded SQL exec statement or the Transact-SQL execute statement.
A unique identifier for the statement, defined in a previous prepare statement.
Specifies the area of memory, or the SQLDA structure, that describes the statement’s dynamic parameter markers or select column list.
An into clause is required when the statement executes a select statement, which must be a single-row select. The target of the into clause can be a SQL descriptor, a SQLDA structure, or a list of one or more Embedded SQL host variables.
Each host variable in the host_var_list must first be defined in a declare section. An indicator variable can be associated with a host variable to show when a null data value is retrieved.
Identifies descriptor_name as a SQLDA structure.
Identifies descriptor_name as a SQL descriptor.
The host variables that are substituted for dynamic parameter markers in host_var_list. The host variables, which you must define in a declare section, are substituted in the order listed. Use this clause only when statement_name contains dynamic parameter markers. The dynamic descriptor can also contain the values for the dynamic parameter markers.
EXEC SQL BEGIN DECLARE SECTION END-EXEC.
01 DEMO-BUF PIC X(100).
01 TITLE-ID PIC X(6).
01 ORDER-NO PIC X(20).
01 QTY PIC S9(9).
EXEC SQL END DECLARE SECTION END-EXEC.
...
MOVE "INSERT salesdetail(ord_num, title_id, qty) VALUES( :?, :?, :?)"
- TO DEMO-BUF.
EXEC SQL PREPARE ins_stmt FROM :DEMO-BUF END-EXEC.
DISPLAY "RECORDING BOOK SALES".
DISPLAY "ORDER # ? ".
ACCEPT ORDER-NO.
DISPLAY "TITLE ID? ".
ACCEPT TITLE-ID.
DISPLAY "QTY SOLD? ".
ACCEPT QTY.
EXEC SQL EXECUTE ins_stmt USING :ORDER-NO, :TITLE-ID, :QTY END-EXEC.
...
execute is the second step in method 2 of dynamic SQL. The first step is the prepare statement.
prepare and execute are valid with any SQL statement except a multirow select statement. For multirow select statements, use either dynamic cursor.
The statement in statement_name can contain dynamic parameter markers (“?”). They mark the positions where host variable values are to be substituted before the statement executes.
The execute keyword distinguishes this statement from exec. See the exec reference page for information on exec.
declare section, get descriptor, prepare, set descriptor