informix
Informix Guide to SQL: Syntax
SQL Statements

PREPARE

Use the PREPARE statement to parse, validate, and generate an execution plan for SQL statements at runtime.

Use this statement with ESQL/C.

Syntax

Element Purpose Restrictions Syntax
statement_id Identifier that represents the data structure of an SQL statement or sequence of SQL statements After you release the database-server resources (using a FREE statement), you cannot use the statement identifier with a DECLARE cursor or with the EXECUTE statement until you prepare the statement again. Identifier, p. 4-205
statement_id_var Host variable that contains the statement identifier This variable must be a character data type. Name must conform to language-specific rules for variable names.
statement_var Host variable whose value is a character string that consists of one or more SQL statements This variable must be a character data type. For restrictions on the statements in the character string, see Restricted Statements in Single-Statement Prepares and Restricted Statements in Multi-Statement Prepares. A statement variable name cannot be used if the SQL statement contains the Collection Derived Table segment. Name must conform to language-specific rules for variable names.

Usage

The PREPARE statement permits your program to assemble the text of an SQL statement at runtime and make it executable. This dynamic form of SQL is accomplished in three steps:

  1. A PREPARE statement accepts statement text as input, either as a quoted string or stored within a character variable. Statement text can contain question-mark (?) placeholders to represent values that are to be defined when the statement is executed.
  2. An EXECUTE or OPEN statement can supply the required input values and execute the prepared statement once or many times.
  3. Resources allocated to the prepared statement can be released later using the FREE statement.

The number of prepared items in a single program is limited by the available memory. These items include both statement identifiers that are named in PREPARE statements (statement_id or statement_id_var) and declared cursors. To avoid exceeding the limit, use a FREE statement to release some statements or cursors.

Restrictions

For information on statements that you cannot use in a prepared statement, see Restricted Statements in Single-Statement Prepares and Restricted Statements in Multi-Statement Prepares.

The maximum length of a PREPARE statement is 64 kilobytes.

Using a Statement Identifier

A PREPARE statement sends the statement text to the database server. The database server analyzes the statement text. If the text contains no syntax errors, the database server translates it to an internal form. This translated statement is saved for later execution in a data structure that the PREPARE statement allocates. The name of the structure is the value that is assigned to the statement identifier in the PREPARE statement. Subsequent SQL statements refer to the structure by using the same statement identifier that was used in the PREPARE statement.

A subsequent FREE statement releases the resources that were allocated to the statement. After you release the database-server resources, you cannot use the statement identifier with a DECLARE cursor or with the EXECUTE statement until you prepare the statement again.

Scope of Statement Identifiers

A program can consist of one or more source-code files. By default, the scope of a statement identifier is global to the program. Therefore, a statement identifier that is prepared in one file can be referenced from another file.

In a multiple-file program, if you want to limit the scope of a statement identifier to the file in which it is prepared, preprocess all the files with the -local command-line option.

Releasing a Statement Identifier

A statement identifier can represent only one SQL statement or sequence of statements at a time. You can execute a new PREPARE statement with an existing statement identifier if you wish to bind a given statement identifier to a different SQL statement text.

The PREPARE statement supports dynamic statement-identifier names, which allow you to prepare a statement identifier as an identifier or as a host character-string variable.

The first example shows a statement identifier that was prepared as a host variable. The second example shows a statement identifier that was prepared as a character-string constant:

A statement-identifier variable must be a character data type. In C, it must be defined as char.

Statement Text

The PREPARE statement can take statement text either as a quoted string or as text that is stored in a program variable. The following restrictions apply to the statement text:

The following ESQL/C example shows a PREPARE statement that takes statement text as a quoted string:

If the prepared statement contains the Collection Derived Table segment on an ESQL/C collection variable, some additional limitations exist on how you can assemble the text for the PREPARE statement. For information about dynamic SQL, see the Informix ESQL/C Programmer's Manual.

Preparing and Executing User-Defined Routines

The way to prepare a user-defined routine (UDR) depends on whether the UDR is a user-defined procedure or a user-defined function:

Restricted Statements in Single-Statement Prepares

In general, you can prepare any database manipulation statement.

You can prepare any single SQL statement except the following statements.

You can prepare any single SQL statement except the following statements

You can prepare a SELECT statement. If the SELECT statement includes the INTO TEMP clause, you can execute the prepared statement with an EXECUTE statement. If it does not include the INTO TEMP clause, the statement returns rows of data. Use DECLARE, OPEN, and FETCH cursor statements to retrieve the rows.

A prepared SELECT statement can include a FOR UPDATE clause. This clause is used with the DECLARE statement to create an update cursor. The following example shows a SELECT statement with a FOR UPDATE clause in ESQL/C:

Preparing Statements When Parameters Are Known

In some prepared statements, all necessary information is known at the time the statement is prepared. The following example in ESQL/C shows two statements that were prepared from constant data:

Preparing Statements That Receive Parameters

In some statements, parameters are unknown when the statement is prepared because a different value can be inserted each time the statement is executed. In these statements, you can use a question-mark (?) placeholder where a parameter must be supplied when the statement is executed.

The PREPARE statements in the following ESQL/C examples show some uses of question-mark (?) placeholders:

You can use a placeholder to defer evaluation of a value until runtime only for an expression. You cannot use a question-mark (?) placeholder to represent an SQL identifier except as noted in Preparing Statements with SQL Identifiers.

The following example of an ESQL/C code fragment prepares a statement from a variable that is named demoquery. The text in the variable includes one question-mark (?) placeholder. The prepared statement is associated with a cursor and, when the cursor is opened, the USING clause of the OPEN statement supplies a value for the placeholder.

The USING clause is available in both OPEN (for statements that are associated with a cursor) and EXECUTE (all other prepared statements) statements.

You can use a question-mark (?) placeholder to represent the name of an ESQL/C or SPL collection variable.

Preparing Statements with SQL Identifiers

In general, you cannot use question-mark (?) placeholders for SQL identifiers. You must specify these identifiers in the statement text when you prepare the statement.

However, in a few special cases, you can use the question-mark (?) placeholder for an SQL identifier. These cases are as follows:

Obtaining SQL Identifiers from User Input

If a prepared statement requires identifiers, but the identifiers are unknown when you write the prepared statement, you can construct a statement that receives SQL identifiers from user input.

The following ESQL/C example prompts the user for the name of a table and uses that name in a SELECT statement. Because the table name is unknown until runtime, the number and data types of the table columns are also unknown. Therefore, the program cannot allocate host variables to receive data from each row in advance. Instead, this program fragment describes the statement into an sqlda descriptor and fetches each row with the descriptor. The fetch puts each row into memory locations that the program provides dynamically.

If a program retrieves all the rows in the active set, the FETCH statement would be placed in a loop that fetched each row. If the FETCH statement retrieves more than one data value (column), another loop exists after the FETCH, which performs some action on each data value.

Preparing Sequences of Multiple SQL Statements

You can execute several SQL statements as one action if you include them in the same PREPARE statement. Multistatement text is processed as a unit; actions are not treated sequentially. Therefore, multistatement text cannot include statements that depend on actions that occur in a previous statement in the text. For example, you cannot create a table and insert values into that table in the same prepared block.

If a statement in a multistatement prepare returns an error, the whole prepared statement stops executing. The database server does not execute any remaining statements.

In most situations, compiled products return error-status information on the error, but do not indicate which statement in the sequence causes an error. You can use the sqlca.sqlerrd[4] field in the sqlca to find the offset of the errors.

In a multistatement prepare, if no rows are returned from a WHERE clause in the following statements, the database server returns the error,
SQLNOTFOUND (100):

In the following example, four SQL statements are prepared into a single ESQL/C string that is called query. Individual statements are delimited with semicolons. A single PREPARE statement can prepare the four statements for execution, and a single EXECUTE statement can execute the statements that are associated with the qid statement identifier.

In the preceding code fragment, the semicolons (;) are required as SQL statement-terminator symbols between each SQL statement in the text that query holds.

Restricted Statements in Multi-Statement Prepares

In addition to the statements listed as exceptions in Restricted Statements in Single-Statement Prepares, you cannot use the following statements in text that contains multiple statements that are separated by semicolons.

In addition, the following types of statements are not allowed in a multistatement prepare:

Using the SELECT Statement in a Multistatement Prepare

In general, you cannot use the SELECT statement in a multistatement prepare. The only form of the SELECT statement allowed in a multistatement prepare is a SELECT statement with an INTO temporary table clause.

Using Prepared Statements for Efficiency

To increase performance efficiency, you can use the PREPARE statement and an EXECUTE statement in a loop to eliminate overhead that redundant parsing and optimizing cause. For example, an UPDATE statement that is located within a WHILE loop is parsed each time the loop runs. If you prepare the UPDATE statement outside the loop, the statement is parsed only once, eliminating overhead and speeding statement execution. The following example shows how to prepare an ESQL/C statement to improve performance:

Related Information

Related statements: CLOSE, DECLARE, DESCRIBE, EXECUTE, FREE, OPEN, SET AUTOFREE, and SET DEFERRED_PREPARE

For information about basic concepts relating to the PREPARE statement, see the Informix Guide to SQL: Tutorial.

For information about more advanced concepts relating to the PREPARE statement, see the Informix ESQL/C Programmer's Manual.


Informix Guide to SQL: Syntax, Version 9.2
Copyright © 1999, Informix Software, Inc. All rights reserved