Updated on 2024-06-03 GMT+08:00

PREPARE

Description

Prepares the statement to be executed.

Syntax

PREPARE name FROM string

Parameters

  • name

    An identifier for the prepared query.

  • string

    A C string or host variable that contains a prepared statement, which can be SELECT, INSERT, UPDATE, or DELETE.

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;

The PREPARE statement provided by ecpg is not equivalent to the PREPARE syntax provided by the kernel. Example:

GaussDB Kernel kernel syntax:
PREPARE name [ ( data_type [, ...] ) ] AS statement
Embedded SQL statement:
EXEC SQL PREPARE I (int, int) AS INSERT INTO T VALUES ( $1, $2 );
EXEC SQL EXECUTE I(1, 2);

When the preceding statement is executed, an error message "too few arguments on" is reported. ecpg provides a dynamic SQL statement to solve the problem in the PREPARE name [ ( data_type [, ...] ) ] AS statement syntax scenario.

Example of using dynamic SQL syntax rules to solve the preceding problem:
EXEC SQL PREPARE I AS INSERT INTO T VALUES ( $1, $2 );
EXEC SQL EXECUTE I using 1, 2;