The EXECUTE IMMEDIATE statement:
EXECUTE IMMEDIATE combines the basic functions of the PREPARE and EXECUTE statements. It can be used to prepare and execute SQL statements that contain neither host variables nor parameter markers.
Invocation
This statement can be embedded only in an application program. It is an executable statement that cannot be dynamically prepared.
Authorization
The authorization rules are those defined for the SQL statement specified by EXECUTE IMMEDIATE.
Syntax
>>-EXECUTE IMMEDIATE--host-variable----------------------------><
The value of the identified host variable is called the statement string.
The statement string must be one of the following SQL statements:
The statement string must not include parameter markers or references to host variables, and must not begin with EXEC SQL. It must not contain a statement terminator with the exception of the CREATE TRIGGER statement which can contain a semi-colon (;) to separate triggered SQL statements.
When an EXECUTE IMMEDIATE statement is executed, the specified statement string is parsed and checked for errors. If the SQL statement is invalid, it is not executed and the error condition that prevents its execution is reported in the SQLCA. If the SQL statement is valid, but an error occurs during its execution, that error condition is reported in the SQLCA.
Notes
Example
Use C program statements to move an SQL statement to the host variable qstring (char[80]) and prepare and execute whatever SQL statement is in the host variable qstring.
if ( strcmp(accounts,"BIG") == 0 ) strcpy (qstring,"INSERT INTO WORK_TABLE SELECT * FROM EMP_ACT WHERE ACTNO < 100"); else strcpy (qstring,"INSERT INTO WORK_TABLE SELECT * FROM EMP_ACT WHERE ACTNO >= 100"); . . . EXEC SQL EXECUTE IMMEDIATE :qstring;