EXECUTE IMMEDIATE is an executable command that can only be embedded in a host language. EXECUTE IMMEDIATE cannot be dynamically prepared.
The authorization required for EXECUTE IMMEDIATE conforms exactly with whatever authorization is needed to dynamically prepare the SQL statement indicated in the statement string argument. Consult individual SQL command descriptions in this reference for authorization requirements.
EXECUTE IMMEDIATE {string-expression | host-variable}
string-expression | A delimited character string that can contain any valid SQL command except CLOSE, DESCRIBE, EXECUTE, EXECUTE IMMEDIATE, FETCH, INCLUDE, OPEN, PREPARE, WHENEVER, or SELECT. |
host-variable | Identifies a variable defined in working storage (according to the rules for declaring character string variables in the host language) that contains a valid string expression (statement string). |
Host variables cannot be used in the string expressions.
EXEC SQL EXECUTE IMMEDIATE 'DELETE FROM employee WHERE fname = "Elvis"' END-EXEC EXEC SQL EXECUTE IMMEDIATE 'UPDATE employee SET fname = "Elvis" WHERE e_no = 1' END-EXEC
Another example of EXECUTE IMMEDIATE embedded in COBOL appears below:
MOVE 'UPDATE employee SET fname = "Elvis" WHERE e_no = 1' TO exstr EXEC SQL EXECUTE IMMEDIATE :exstr END-EXEC
Description
The EXECUTE IMMEDIATE command prepares and executes non-SELECT SQL commands within a statement string. You should check the syntax of the SQL statement you plan to use before placing that statement into an EXECUTE IMMEDIATE command. This check can be performed with SQLWizard.
The statement string cannot contain parameter markers (question marks) or references to host variables. The statement cannot begin with EXEC SQL or end with END-EXEC (or a semicolon). If the SQL command appearing in the statement string is invalid, an error condition is reported to the SQLCA.
The statement string must be one of the following SQL statements: