Using PREPARE and EXECUTE
Go Up to Method 1: Non-query Statements Without Parameters (Embedded SQL Guide)
To execute a statement string several times, use PREPARE
and EXECUTE
:
- Elicit a statement string from the user or create one that contains the SQL statement to be processed. For example, the following statement creates a SQL statement string:
char *str = "UPDATE DEPARTMENT SET BUDGET = BUDGET * 1.05";
- Parse and name the statement string with
PREPARE
. The name is used in subsequent calls toEXECUTE
:EXEC SQL PREPARE SQL_STMT FROM :str;
SQL_STMT is the name assigned to the parsed statement string. - Execute the named statement string using
EXECUTE
. For example, the following statement executes a statement string named SQL_STMT:EXEC SQL EXECUTE SQL_STMT;
- Note:
PREPARE
also accepts string literals. For example,
EXEC SQL PREPARE SQL_STMT FROM 'UPDATE DEPARTMENT SET BUDGET = BUDGET * 1.05';
Once a statement string is prepared, it can be executed as many times as required in an application.
- Note: