Preparing the SQL Statement
The STRING
created in the query-by-example is not valid for
execution. The PREPARE
instruction sends the text of the string to the
database server for parsing, validation, and to generate the execution plan.
The scope of a prepared SQL statement is the module in which it is
declared.
PREPARE cust_cnt_stmt FROM sql_text
A prepared SQL statement can be executed with the
EXECUTE
instruction.EXECUTE cust_cnt_stmt INTO cust_cnt
Since the SQL statement will only return one row (containing the count) the INTO
syntax of the EXECUTE
instruction can be used to store the count in the
local variable cust_cnt
. (The function cust_select
illustrates the use of database cursors with SQL SELECT
statements.)
When a prepared statement is no longer needed, the
FREE
instruction will release
the resources associated with the
statement.FREE cust_cnt_stmt