Databases Reference
In-Depth Information
If an SQL statement does not contain host variables, nor parameter markers, and
it does not return a result set, then the EXECUTE IMMEDIATE statement can be
used. This statement prepares and executes a statement in one step. For
example, in our inventory program, we need to update information about a
product in the INVENTORY table as specified by the user. Because the column
to be updated is unknown prior to precompile time, dynamic SQL must be used
and we need to prepare the statement. Once we get the column to be updated
and its value from the user, we can build the SQL string and then use the
EXECUTE IMMEDIATE statement to prepare and execute in one step. This is
shown in Example 4-22. A sample user session is shown in Example 4-23 on
page 169. The completed update_product() function can be found in the
complete program definition shown in Example 4-24 on page 169.
Example 4-22 Updating a column using EXECUTE IMMEDIATE
if (option==UPDATE_LOCATION) // Ensure single quotes enclose the
location value
sprintf(updateStmt,"update inventory set %s='%s' where PID='%s'",
updateField, updateValue, PID);
else
sprintf(updateStmt,"update inventory set %s=%s where PID='%s'",
updateField, updateValue, PID);
EXEC SQL EXECUTE IMMEDIATE :updateStmt;
EXEC SQL COMMIT;
Search WWH ::




Custom Search