Was this helpful?
Prepare and Execute Statements
The PREPARE statement tells OpenSQL 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 in a transaction by issuing the EXECUTE statement and specifying the statement name.
If your program executes the same statement many times in a transaction, the prepare and execute method can improve the performance of the statement. Committing a transaction discards any statements that were prepared during the transaction.
The following OpenSQL statements cannot be prepared:
CALL
EXECUTE
CLOSE
FETCH
CONNECT
INCLUDE
DECLARE
INQUIRE_SQL
DESCRIBE
OPEN
DISCONNECT
SET
EXECUTE IMMEDIATE
WHENEVER
The PREPARE statement has the following syntax:
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 as follows:
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 (?). The portions specified using question marks must be filled in by the USING clause when the statement is executed.
Last modified date: 11/09/2022