Updated on 2024-05-07 GMT+08:00

Executing a Statement with Input Parameters

Prepare a normal statement and execute a specific version of it by replacing its parameters (with question marks). Use the EXECUTE statement to execute the prepared statement by specifying parameters in the USING clause. Example:
EXEC SQL BEGIN DECLARE SECTION;
    const char *stmt = "INSERT INTO test1 VALUES(?, ?);";
EXEC SQL END DECLARE SECTION;
    /* PREPARE Prepare a statement for execution. */
    EXEC SQL PREPARE mystmt FROM :stmt;
    ...
    /* Single quotation marks are valid characters. If a character string is used, use double quotation marks.*/
    EXEC SQL EXECUTE mystmt USING 42, 'foobar';

    /* When you no longer need a prepared statement, you should deallocate it.*/
    EXEC SQL DEALLOCATE PREPARE name;