PREPARE Statement [ESQL]

Prepares a statement to be executed later or used for a cursor.

Syntax

PREPARE statement-name 
FROM statementFORREAD ONLY | UPDATEOF column-name-list ] } ] 
... [ DESCRIBE describe-type INTO [ [ SQL ] DESCRIPTOR ] descriptor ]
... [ WITH EXECUTE ]

Parameters

Examples

Usage

The PREPARE statement prepares a SQL statement from the statement and associates the prepared statement with statement-name.

This statement name is referenced to execute the statement, or to open a cursor if the statement is a SELECT statement. statement-name may be a host variable of type a_sql_statement_number defined in the sqlca.h header file that is automatically included. If an identifier is used for the statement-name, only one statement per module may be prepared with this statement-name.

If a host variable is used for statement-name, it must have the type short int. There is a typedef for this type in sqlca.h called a_sql_statement_number. This type is recognized by the SQL preprocessor and can be used in a DECLARE section. The host variable is filled in by the database during the PREPARE statement and need not be initialized by the programmer.

FOR UPDATE | FOR READ ONLY Defines the cursor updatability if the statement is used by a cursor. A FOR READ ONLY cursor cannot be used in an UPDATE (positioned) or a DELETE (positioned) operation. FOR READ ONLY is the default. In response to any request for a cursor that specifies FOR UPDATE, SAP Sybase IQ provides either a value-sensitive cursor or a sensitive cursor. Insensitive and asensitive cursors are not updatable.

If the DESCRIBE INTO DESCRIPTOR clause is used, the prepared statement is described into the specified descriptor. The describe type may be any of the describe types allowed in the DESCRIBE statement.

If the WITH EXECUTE clause is used, the statement is executed if and only if it is not a CALL or SELECT statement, and it has no host variables. The statement is immediately dropped after a successful execution. If PREPARE and DESCRIBE (if any) are successful but the statement cannot be executed, a warning SQLCODE 111, SQLSTATE 01W08 is set, and the statement is not dropped.

The DESCRIBE INTO DESCRIPTOR and WITH EXECUTE clauses might improve performance, as they decrease the required client/server communication.

The WITH VARIABLE RESULT clause is used to describe procedures that may have more than one result set, with different numbers or types of columns.

If WITH VARIABLE RESULT is used, the database server sets the SQLCOUNT value after the describe to one of these values:

  • 0—The result set may change: the procedure call should be described again following each OPEN statement.

  • 1—The result set is fixed. No redescribing is required.

These statements can be prepared:

  • ALTER

  • CALL

  • COMMENT ON

  • CREATE

  • DELETE

  • DROP

  • GRANT

  • INSERT

  • REVOKE

  • SELECT

  • SET OPTION

Preparing COMMIT, PREPARE TO COMMIT, and ROLLBACK statements is still supported for compatibility. However, perform all transaction management operations with static Embedded SQL, because certain application environments may require it. Also, other Embedded SQL systems do not support dynamic transaction management operations.

Note: Make sure that you DROP the statement after use. If you do not, then the memory associated with the statement is not reclaimed.
Side Effects
  • Any statement previously prepared with the same name is lost.

Standards

  • SQL—ISO/ANSI SQL compliant.

  • Sybase—Supported by Open Client/Open Server.

Permissions

None

Related reference
DECLARE CURSOR Statement [ESQL] [SP]
DESCRIBE Statement [ESQL]
DROP Statement
EXECUTE Statement [ESQL]
OPEN Statement [ESQL] [SP]