PREPARE
Go Up to Statement and Function Reference (Language Reference Guide)
Prepares a dynamic SQL (DSQL) statement for execution. Available in gpre.
PREPARE [TRANSACTION transaction] statement
[INTO SQL DESCRIPTOR xsqlda] FROM {:variable | 'string'};
| Argument | Description |
|---|---|
|
|
Name of the transaction under control of which the statement is executed. |
|
<statement> |
Establishes an alias for the prepared statement that can be used by subsequent |
|
|
Specifies an |
|
<variable> | `<string>’ |
|
Description: PREPARE readies a DSQL statement for repeated execution by:
- Checking the statement for syntax errors.
- Determining data types of optionally specified dynamic parameters.
- Optimizing statement execution.
- Compiling the statement for execution by
EXECUTE.
PREPARE is part of a group of statements that prepare DSQL statements for execution.
| Statement | Purpose |
|---|---|
|
|
Readies a |
|
|
Fills in the |
|
|
Executes a previously prepared statement. |
|
|
Prepares a |
After a statement is prepared, it is available for execution as many times as necessary during the current session. To prepare and execute a statement only once, use EXECUTE IMMEDIATE.
<statement> establishes a symbolic name for the actual DSQL statement to prepare. It is not declared as a host-language variable. Except for C programs, gpre does not distinguish between uppercase and lowercase in <statement>, treating “B” and “b” as the same character. For C programs, use the gpre-either_case switch to activate case sensitivity during preprocessing.
If the optional INTO clause is used, PREPARE also fills in the extended SQL descriptor area (XSQLDA) with information about the data type, length, and name of select-list columns in the prepared statement. This clause is useful only when the statement to prepare is a SELECT.
The DESCRIBE statement can be used instead of the INTO clause to fill in the XSQLDA for a select list.
The FROM clause specifies the actual DSQL statement to PREPARE. It can be a host-language variable, or a quoted string literal. The DSQL statement to PREPARE can be any SQL data definition, data manipulation, or transaction-control statement.
Examples: The following embedded SQL statement prepares a DSQL statement from a host-variable statement. Because it uses the optional INTO clause, the assumption is that the DSQL statement in the host variable is a SELECT.
EXEC SQL
PREPARE Q INTO xsqlda FROM :buf;
The previous statement could also be prepared and described in the following manner:
EXEC SQL
PREPARE Q FROM :buf;
EXEC SQL
DESCRIBE Q INTO SQL DESCRIPTOR xsqlda;