5. Working with Embedded SQL : Dynamic Programming : Dynamic SQL Statements : Prepare and Execute Statements
 
Share this page                  
Prepare and Execute Statements
The PREPARE statement tells the DBMS Server to encode the dynamically built statement and assign it the specified name. After a statement is prepared, the program can execute the statement one or more times within a transaction by issuing the EXECUTE statement and specifying the statement name. This method improves performance if your program must execute the same statement many times in a transaction. When a transaction is committed, all statements that were prepared during the transaction are discarded.
The following SQL statements cannot be prepared:
CALL
CLOSE
CONNECT
DECLARE
DISCONNECT
ENDDATA
EXECUTE IMMEDIATE
EXECUTE FETCH
GET DATA
GET DBEVENT
INCLUDE
INQUIRE_SQL
OPEN
PREPARE TO COMMIT
PREPARE
PUT DATA
SET
SET_SQL
WHENEVER
The syntax of the PREPARE statement is:
EXEC SQL PREPARE statement_name
        
[INTO descriptor_name|USING DESCRIPTOR descriptor_name]
        FROM host_string_variable | string_literal;
The statement_name can be a string literal or variable. The contents of the host string variable or the string literal cannot include EXEC SQL or the statement terminator.
If the INTO clause is included in the PREPARE statement, the PREPARE statement also describes the statement string into the specified descriptor area and it is not necessary to describe the statement string separately.
The syntax of the EXECUTE statement is:
EXEC SQL EXECUTE statement_name
        
[USING host_variable {, host_variable}
         | USING DESCRIPTOR descriptor_name];
A prepared statement can be fully specified, or some portions can be specified by question marks (?); these elements must be filled in (by the USING clause) when the statement is executed. For more information see Prepare in the chapter “SQL Statements.”