Method 1: Non-query Statements Without Parameters (Embedded SQL Guide)
From InterBase
Go Up to DSQL Programming Methods (Embedded SQL Guide)
There are two ways to process a SQL statement string containing a non-query statement without placeholder parameters:
- Use
EXECUTE IMMEDIATEto prepare and execute the string a single time. - Use
PREPAREto parse the statement for execution and assign it a name, then useEXECUTEto carry out the statement’s actions as many times as required in an application.
Using EXECUTE IMMEDIATE
- To execute a statement string a single time, use
EXECUTE IMMEDIATE: - 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";
- 3. Parse and execute the statement string using
EXECUTE IMMEDIATE:
EXEC SQL EXECUTE IMMEDIATE :str;
Note:
EXECUTE IMMEDIATE also accepts string literals. For example,EXEC SQL EXECUTE IMMEDIATE 'UPDATE DEPARTMENT SET BUDGET = BUDGET * 1.05';
Using PREPARE and EXECUTE
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:PREPAREalso 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.