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 IMMEDIATE to prepare and execute the string a single time.
  • Use PREPARE to parse the statement for execution and assign it a name, then use EXECUTE to carry out the statement’s actions as many times as required in an application.

Using EXECUTE IMMEDIATE

  1. To execute a statement string a single time, use EXECUTE IMMEDIATE:
  2. 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:

  1. 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";
    
  2. Parse and name the statement string with PREPARE. The name is used in subsequent calls to EXECUTE:
    EXEC SQL
    PREPARE SQL_STMT FROM :str;
    
    SQL_STMT is the name assigned to the parsed statement string.
  3. 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.

Advance To: