informix
Informix Guide to SQL: Syntax
SQL Statements

EXECUTE

Use the EXECUTE statement to run a previously prepared statement or set of statements.

Use this statement with ESQL/C.

Syntax

Element Purpose Restrictions Syntax
statement_id Identifier for a prepared SQL statement You must have defined the statement identifier in a previous PREPARE statement. 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. PREPARE, p. 2-579
statement_id_var Host variable that identifies an SQL statement You must have defined the host variable in a previous PREPARE statement The host variable must be a character data type. PREPARE, p. 2-579

Usage

The EXECUTE statement passes a prepared SQL statement to the database server for execution. The following example shows an EXECUTE statement within an ESQL/C program:

Once prepared, an SQL statement can be executed as often as needed.

If the statement contained question mark (?) placeholders, you use the USING clause to provide specific values for them before execution. For more information, see the USING Clause.

You can execute any prepared statement except those in the following list:

If you create or drop a trigger after you prepared a triggering INSERT, DELETE, or UPDATE statement, the prepared statement returns an error when you execute it.

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, so a statement identifier created 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 executed, you can preprocess all the files with the -local command-line option.

The sqlca Record and EXECUTE

Following an EXECUTE statement, the sqlca can reflect two results:

Error Conditions with EXECUTE

If a prepared statement fails to access any rows, the database server returns zero (0). In a multistatement prepare, if any statement in the following list fails to access rows, the database server returns SQLNOTFOUND (100):

In an ANSI-compliant database, if you prepare and execute any of the statements in the preceding list, and no rows are returned, the database server returns SQLNOTFOUND (100).

INTO Clause

Use the INTO clause to save the return values of the following SQL statements:

The INTO clause provides a concise and efficient alternative to more complicated and lengthy syntax. In addition, by placing values into variables that can be displayed, the INTO clause simplifies and enhances your ability to retrieve and display data values. For example, if you use the INTO clause, you do not have to use a cursor to retrieve values from a table.

You can store the returned values into output variables, output SQL descriptors, or output sqlda pointers.

Restrictions with the INTO Clause

If you execute a prepared SELECT statement that returns more than one row of data or a prepared EXECUTE FUNCTION (or EXECUTE PROCEDURE) statement for an SPL function that returns more than one group of return values, you receive an error message. In addition, if you prepare and declare a statement, and then attempt to execute that statement, you receive an error message.

You cannot select a null value from a table column and place that value into an output variable. If you know in advance that a table column contains a null value, after you select the data, check the indicator variable that is associated with the column to determine if the value is null.

The following list describes the procedure for how to use the INTO clause with the EXECUTE statement:

  1. Declare the output variables that the EXECUTE statement uses.
  2. Use the PREPARE statement to prepare your SELECT statement or to prepare your EXECUTE FUNCTION (or EXECUTE PROCEDURE) statement.
  3. Use the EXECUTE statement, with the INTO clause, to execute your SELECT statement or to execute your EXECUTE FUNCTION (or EXECUTE PROCEDURE) statement.

Storage Location for Returned Values

You can specify any of the following items to replace the question-mark placeholders in a statement before you execute it:

Saving Values In Host or Program Variables

If you know the number of return values to be supplied at runtime and their data types, you can define the values that the SELECT or EXECUTE FUNCTION (or EXECUTE PROCEDURE) statement returns as host variables in your program. You use these host variables with the INTO keyword, followed by the names of the variables. These variables are matched with the return values in a one-to-one correspondence, from left to right.

You must supply one variable name for each value that the SELECT or EXECUTE FUNCTION (or EXECUTE PROCEDURE) returns. The data type of each variable must be compatible with the corresponding return value of the prepared statement.

Saving Values in a System-Descriptor Area

If you do not know the number of return values to be supplied at runtime or their data types, you can associate output values with a system-descriptor area. A system-descriptor area describes the data type and memory location of one or more values.

A system-descriptor area conforms to the X/Open standards.

To specify a system-descriptor area as the location of output values, use the INTO SQL DESCRIPTOR clause of the EXECUTE statement. Each time that the EXECUTE statement is run, the values that the system-descriptor area describes are stored in the system-descriptor area.

The following example shows how to use the system-descriptor area to execute prepared statements in Informix ESQL/C:

The COUNT field corresponds to the number of values that the prepared statement returns. The value of COUNT must be less than or equal to the value of the occurrences that were specified when the system-descriptor area was allocated with the ALLOCATE DESCRIPTOR statement. You can obtain the value of a field with the GET DESCRIPTOR statement and set the value with the SET DESCRIPTOR statement.

For further information, refer to the discussion of the system-descriptor area in the Informix ESQL/C Programmer's Manual.

Saving Values in an sqlda Structure

If you do not know the number of output values to be returned at runtime or their data types, you can associate output values from an sqlda structure. An sqlda structure lists the data type and memory location of one or more return values. To specify an sqlda structure as the location of return values, use the INTO DESCRIPTOR clause of the EXECUTE statement. Each time the EXECUTE statement is run, the database server places the returns values that the sqlda structure describes into the sqlda structure.

The following example shows how to use an sqlda structure to execute a prepared statement in Informix ESQL/C:

The sqld value specifies the number of output values that are described in occurrences of sqlvar. This number must correspond to the number of values that the SELECT or EXECUTE FUNCTION (or EXECUTE PROCEDURE) statement returns.

For more information, refer to the sqlda discussion in the Informix ESQL/C Programmer's Manual.

Examples

The following example shows how to use the INTO clause with an EXECUTE statement in ESQL/C:

The following example shows how to use the INTO clause to return multiple rows of data:

USING Clause

Use the USING clause to specify the values that are to replace question-mark (?) placeholders in the prepared statement. Providing values in the EXECUTE statement that replace the question-mark placeholders in the prepared statement is sometimes called parameterizing the prepared statement.

Element Purpose Restrictions Syntax
descriptor Quoted string that identifies a system-descriptor area System-descriptor area must already be allocated Make sure surrounding quotation marks are single. Quoted String, p. 4-260
descriptor_var Host variable that identifies a system-descriptor area System-descriptor area must already be allocated Name must conform to language-specific rules for variable names.
indicator_var Host variable that receives a return code if null data is placed in the corresponding parameter_var This variable receives truncation information if truncation occurs. Variable cannot be DATETIME or INTERVAL data type Name must conform to language-specific rules for variable names.
parameter_var Host variable whose contents replace a question-mark (?) placeholder in a prepared statement Variable must be a character data type Name must conform to language-specific rules for variable names.
sqlda_pointer Pointer to an sqlda structure that defines the data type and memory location of values that correspond to the question-mark (?) placeholder in a prepared statement You cannot begin sqlda_pointer with a dollar sign ($) or a colon (:). You must use an sqlda structure if you are using dynamic SQL statements. DESCRIBE, p. 2-382

If you know the number of parameters to be supplied at runtime and their data types, you can define the parameters that are needed by the statement as host variables in your program.

If you do not know the number of parameters to be supplied at runtime or their data types, you can associate input values from a system-descriptor area or an sqlda structure. Both of these descriptor structures describe the data type and memory location of one or more values to replace question-mark (?) placeholders.

Supplying Parameters Through Host or Program Variables

You pass parameters to the database server by opening the cursor with the USING keyword, followed by the names of the variables. These variables are matched with prepared statement question-mark (?) placeholders in a one-to-one correspondence, from left to right. You must supply one storage parameter variable for each placeholder. The data type of each variable must be compatible with the corresponding value that the prepared statement requires.

The following example executes the prepared UPDATE statement in ESQL/C:

Supplying Parameters Through a System Descriptor

You can create a system-descriptor area that describes the data type and memory location of one or more values and then specify the descriptor in the USING SQL DESCRIPTOR clause of the EXECUTE statement.

Each time that the EXECUTE statement is run, the values that the system-descriptor area describes are used to replace question-mark (?) placeholders in the PREPARE statement.

The COUNT field corresponds to the number of dynamic parameters in the prepared statement. The value of COUNT must be less than or equal to the number of item descriptors that were specified when the system-descriptor area was allocated with the ALLOCATE DESCRIPTOR statement.

The following example shows how to use system descriptors to execute a prepared statement in ESQL/C:

Supplying Parameters Through an sqlda Structure

You can specify the sqlda pointer in the USING DESCRIPTOR clause of the EXECUTE statement.

Each time the EXECUTE statement is run, the values that the descriptor structure describes are used to replace question-mark (?) placeholders in the PREPARE statement.

The sqld value specifies the number of input values that are described in occurrences of sqlvar. This number must correspond to the number of dynamic parameters in the prepared statement.

The following example shows how to use an sqlda structure to execute a prepared statement in ESQL/C:

Related Information

Related statements: ALLOCATE DESCRIPTOR, DEALLOCATE DESCRIPTOR, DECLARE, EXECUTE IMMEDIATE, FETCH, GET DESCRIPTOR, PREPARE, PUT, and SET DESCRIPTOR

For a task-oriented discussion of the EXECUTE statement, see the Informix Guide to SQL: Tutorial.

For more information about concepts relating to the EXECUTE statement, refer to the Informix ESQL/C Programmer's Manual.


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