IBM Books

SQL Reference

EXECUTE

The EXECUTE statement executes a prepared SQL statement.

Invocation

This statement can be embedded only in an application program. It is an executable statement that cannot be dynamically prepared.

Authorization

For statements where authorization checking is performed at statement execution time (DDL, GRANT, and REVOKE statements), the privileges held by the authorization ID of the statement must include those required to execute the SQL statement specified by the PREPARE statement. For statements where authorization checking is performed at statement preparation time (DML), no authorization is required to use this statement.

Syntax

>>-EXECUTE--statement-name-------------------------------------->
 
>-----+------------------------------------+-------------------><
      |        .-,----------------.        |
      |        V                  |        |
      +-USING-----host-variable---+--------+
      '-USING DESCRIPTOR--descriptor-name--'
 

Description

statement-name
Identifies the prepared statement to be executed. The statement-name must identify a statement that was previously prepared and the prepared statement must not be a SELECT statement.

USING
Introduces a list of host variables for which values are substituted for the parameter markers (question marks) in the prepared statement. (For an explanation of parameter markers, see PREPARE.) If the prepared statement includes parameter markers, USING must be used.

host-variable, ...
Identifies a host variable that is declared in the program in accordance with the rules for declaring host variables. The number of variables must be the same as the number of parameter markers in the prepared statement. The nth variable corresponds to the nth parameter marker in the prepared statement. Locator variables and file reference variables, where appropriate, can be provided as the source of values for parameter markers.

DESCRIPTOR descriptor-name
Identifies an input SQLDA that must contain a valid description of host variables.

Before the EXECUTE statement is processed, the user must set the following fields in the input SQLDA:

  • SQLN to indicate the number of SQLVAR occurrences provided in the SQLDA

  • SQLDABC to indicate the number of bytes of storage allocated for the SQLDA

  • SQLD to indicate the number of variables used in the SQLDA when processing the statement

  • SQLVAR occurrences to indicate the attributes of the variables.

The SQLDA must have enough storage to contain all SQLVAR occurrences. Therefore, the value in SQLDABC must be greater than or equal to 16 + SQLN*(N), where N is the length of an SQLVAR occurrence.

If LOB input data needs to be accommodated, there must be two SQLVAR entries for every parameter marker.

SQLD must be set to a value greater than or equal to zero and less than or equal to SQLN. For more information, see Appendix C, SQL Descriptor Area (SQLDA).

Notes

Examples

Example 1:  In this C example, an INSERT statement with parameter markers is prepared and executed. h1 - h4 are host variables that correspond to the format of TDEPT.

   strcpy (s,"INSERT INTO TDEPT VALUES(?,?,?,?)");
   EXEC SQL PREPARE DEPT_INSERT FROM :s;
      .
      .
   (Check for successful execution and put values into :h1, :h2, :h3, :h4)
      .
      .
   EXEC SQL EXECUTE DEPT_INSERT USING :h1, :h2,
   :h3, :h4;

Example 2:  This EXECUTE statement uses an SQLDA.

   EXECUTE S3 USING DESCRIPTOR :sqlda3


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]

[ DB2 List of Books | Search the DB2 Books ]