Dynamic invocation of a SELECT statement
Your application program can dynamically build a SELECT statement in the form of a character string placed in a host variable.
The statement so constructed can be prepared for execution by means of the (embedded) statement PREPARE, and referred to by a (nonexecutable) statement DECLARE CURSOR. The statement is then executed every time you open the cursor by means of the (embedded) statement OPEN. After the cursor is open, you can retrieve the result table a row at a time by successive executions of the (embedded) SQL FETCH statement.
The SELECT statement used in that way must not contain references
to host variables. It can instead contain parameter markers. (See Notes
in PREPARE for rules concerning
parameter markers.) The parameter markers are effectively replaced
by the values of the host variables specified in the OPEN statement.
(See OPEN for rules
concerning this replacement.)
The successful or unsuccessful execution of the SELECT statement is indicated by the values returned in the SQLIMSCODE and SQLIMSSTATE fields in the SQLIMSCA after the OPEN. You should check the fields as described above for embedded statements.