PREPARE

PREPARE — prepare a statement for execution

Synopsis

PREPARE name FROM string

Description

PREPARE prepares a statement dynamically specified as a string for execution. This is different from the direct SQL statement PREPARE, which can also be used in embedded programs. The EXECUTE command is used to execute either kind of prepared statement.

Parameters

prepared_name

An identifier for the prepared query.

string

A literal C string or a host variable containing a preparable statement, one of the SELECT, INSERT, UPDATE, or DELETE.

Enhancement

PREPARE statements are supported to contain comments ("/*" and "--" forms), double quotes; Statements can be anonymous blocks with binding parameters. There are two supported formats for bindings (:variable or : variable).

Examples

char *stmt = "SELECT * FROM test1 WHERE a = ? AND b = ?";

EXEC SQL ALLOCATE DESCRIPTOR outdesc;
EXEC SQL PREPARE foo FROM :stmt;

EXEC SQL EXECUTE foo USING SQL DESCRIPTOR indesc INTO SQL DESCRIPTOR outdesc;

PREPARE anonymous blocks with bound parameters to contain comments ("/*" and "--" forms), double quotes.

    snprintf(sSQLExpress,sizeof(sSQLExpress),
                        " begin \n"
                        "-- $1,$2 \n /* 'zhangsan' \" lisi \" $1,$2*/ \n"
                        " insert into test \" $1 \" (a, b, c) "
                        "  /* $1,$2,$3,$4 */values (:V1, :V2, :V3);"
                        "   commit; "
                        " exception when others then "
                        "   rollback; "
                        "   raise; "
                        " end; " );
    EXEC SQL prepare crN1 from :sSQLExpress;
    EXEC SQL EXECUTE crN1 USING 17, 17, 17;

Compatibility

PREPARE is specified in the SQL standard.

See Also

EXECUTE