DB2 Version 9.7 for Linux, UNIX, and Windows

PREPARE statement

The PREPARE statement is used by application programs to dynamically prepare an SQL statement for execution. The PREPARE statement creates an executable SQL statement, called a prepared statement, from a character string form of the statement, called a statement string.

Invocation

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

Authorization

For statements where authorization checking is performed at statement preparation time (DML), the privileges held by the authorization ID of the statement must include those required to execute the SQL statement specified by the PREPARE statement. The authorization ID of the statement might be affected by the DYNAMICRULES bind option.

For statements where authorization checking is performed at statement execution time (DDL, GRANT, and REVOKE statements), no authorization is required to use this statement; however, the authorization is checked when the prepared statement is executed.

For statements involving tables that are protected with a security policy, the rules associated with the security policy are always evaluated at statement execution time.

If the authorization ID of the statement holds EXPLAIN, SQLADM, or DBADM authority, the user may prepare any statement; however, the ability to execute the statement is re-checked at statement execution time.

Syntax

Read syntax diagramSkip visual syntax diagram
>>-PREPARE--statement-name-------------------------------------->

>--+------------------------------------------+----------------->
   | .-OUTPUT-.                               |   
   '-+--------+--INTO--result-descriptor-name-'   

>--+-----------------------------------+------------------------>
   '-INPUT INTO--input-descriptor-name-'   

>--FROM--+-host-variable-+-------------------------------------><
         '-expression----'   

Description

statement-name
Names the prepared statement. If the name identifies an existing prepared statement, that previously prepared statement is destroyed. The name must not identify a prepared statement that is the SELECT statement of an open cursor.
OUTPUT INTO
If OUTPUT INTO is used, and the PREPARE statement executes successfully, information about the output parameter markers in the prepared statement is placed in the SQLDA specified by result-descriptor-name.
result-descriptor-name
Specifies the name of an SQLDA. (The DESCRIBE statement may be used as an alternative to this clause.)
INPUT INTO
If INPUT INTO is used, and the PREPARE statement executes successfully, information about the input parameter markers in the prepared statement is placed in the SQLDA specified by input-descriptor-name. Input parameter markers are always considered nullable, regardless of usage.
input-descriptor-name
Specifies the name of an SQLDA. (The DESCRIBE statement may be used as an alternative to this clause.)
FROM
Introduces the statement string. The statement string is the value of the specified host variable.
host-variable
Specifies a host variable that is described in the program in accordance with the rules for declaring character string variables. It must be a fixed-length or varying-length character-string variable that is less than the maximum statement size of 2 097 152 bytes. Note that a CLOB(2097152) can contain a maximum size statement, but a VARCHAR cannot.
expression
An expression specifying the statement string. The expression must return a fixed-length or varying-length character-string type that is less than the maximum statement size of 2 097 152 bytes.

Rules

Notes

Examples

Example 1:  Prepare and execute a non-select-statement in a COBOL program. Assume the statement is contained in a host variable HOLDER and that the program will place a statement string into the host variable based on some instructions from the user. The statement to be prepared does not have any parameter markers.
EXEC SQL  PREPARE STMT_NAME FROM :HOLDER
END-EXEC.
EXEC SQL  EXECUTE STMT_NAME
END-EXEC.
Example 2:  Prepare and execute a non-select-statement as in example 1, except code it for a C program. Also assume the statement to be prepared can contain any number of parameter markers.
EXEC SQL  PREPARE STMT_NAME FROM :holder;
EXEC SQL  EXECUTE STMT_NAME USING DESCRIPTOR :insert_da;
Assume that the following statement is to be prepared:
INSERT INTO DEPT VALUES(?, ?, ?, ?)

The columns in the DEPT table are defined as follows:

DEPT_NO   CHAR(3) NOT NULL, -- department number
DEPTNAME  VARCHAR(29), -- department name
MGRNO     CHAR(6), -- manager number
ADMRDEPT  CHAR(3)  -- admin department number

To insert department number G01 named COMPLAINTS, which has no manager and reports to department A00, the structure INSERT_DA should have the values in Table 1 before issuing the EXECUTE statement.

Table 1. Required values for the INSERT_DA structure
SQLDA field Value
SQLDAID SQLDA
SQLDABC 192 (See note 1.)
SQLN 4
SQLD 4
   
SQLTYPE 452
SQLLEN 3
SQLDATA pointer to G01
SQLIND (See note 2.)
SQLNAME  
   
SQLTYPE 449
SQLLEN 29
SQLDATA pointer to COMPLAINTS
SQLIND pointer to 0
SQLNAME  
   
SQLTYPE 453
SQLLEN 6
SQLDATA (See note 3.)
SQLIND pointer to -1
SQLNAME  
   
SQLTYPE 453
SQLLEN 3
SQLDATA pointer to A00
SQLIND pointer to 0
SQLNAME  
Note:
  1. This value is for a PREPARE done from a 32-bit application. If the PREPARE was done in a 64-bit application, then SQLDABC would have the value 240.
  2. The value in SQLIND for this SQLVAR is ignored because the SQLTYPE identifies a non-nullable data type.
  3. The value in SQLDATA for this SQLVAR is ignored because the value of SQLIND indicates this is a null value.