EXECUTE

The EXECUTE statement executes a prepared SQL statement.

Invocation

This statement can be embedded only in a COBOL application program. It is an executable statement that cannot be dynamically prepared.

Syntax

Read syntax diagramSkip visual syntax diagramEXECUTEstatement-nameUSING,host-variable

Description

The following keyword parameters are defined for the EXECUTE statement:
statement-name
Identifies the prepared statement to be executed. statement-name must identify a statement that was previously prepared within the unit of work and the prepared statement must not be a SELECT statement.
USING
Introduces a list of variables whose values are substituted for the parameter markers (question marks) in the prepared statement. (For an explanation of parameter markers, see PREPARE.) If the prepared statement includes parameter markers, you must include USING in the EXECUTE statement. USING is ignored if there are no parameter markers.

For more on the substitution of values for parameter markers, see Parameter marker replacement.

host-variable
Identifies structures or variables that must be described in the application program in accordance with the rules for declaring host structures and variables. A reference to a structure is replaced by a reference to each of its variables. The number of variables must be the same as the number of parameter markers in the prepared statement. The nth variable supplies the value for the nth parameter marker in the prepared statement.

Notes

Parameter marker replacement:
Before the prepared statement is executed, each parameter marker in the statement is effectively replaced by its corresponding host variable. The replacement is an assignment operation in which the source is the value of the host variable and the target is a variable. The assignment rules are those described for assignment to a column in Assignment and comparison.

Example

In this example, an UPDATE statement is prepared from the variable SQLSTMT and executed.

EXEC SQLIMS
  DELCARE UPD STATEMENT
END-EXEC.

EXEC SQLIMS
  PREPARE UPD FROM :SQLSTMT
END-EXEC.
IF SQLIMSCODE < 0
  MOVE ‘**** PREPARE ERROR ****’ TO ERR-MSG1
  PERFORM 100-ERROR
ELSE
  EXEC SQLIMS  
    EXECUTE UPD
  END-EXEC
END-IF.