Updated on 2024-06-03 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. The following is an 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';

    /* If a prepared statement is no longer used, release it in time. */
    EXEC SQL DEALLOCATE PREPARE name;