Most applications will use the steps below to prepare and execute a dynamic SQL statement:
Prepare the dynamic SQL statement.
ct_dynamic(CS_PREPARE)
ct_send
ct_results, in a loop
The prepare command returns no fetchable results.
(Optional) Get a description of the parameters required to execute the prepared statement.
ct_dynamic(CS_DESCRIBE_INPUT)
ct_send
ct_results, in a loop
ct_results returns with a result_type of CS_DESCRIBE_RESULT to indicate that the parameter descriptions are available.
(Optional) Get a description of the result columns returned by the prepared statement.
ct_dynamic(CS_DESCRIBE_OUTPUT)
ct_send
ct_results, in a loop
ct_results returns with a result_type of CS_DESCRIBE_RESULT to indicate that the description is available.
Execute the prepared statement or declare and open a cursor on the prepared statement.
To execute the prepared statement (without a cursor):
ct_dynamic(CS_EXECUTE).
If necessary, define parameter values with ct_param, ct_setparam, ct_dyndesc, or ct_dynsqlda.
ct_send.
ct_results, in a loop. Fetchable results may require processing.
For a description of how to execute a prepared statement with a cursor, see “Using Client-Library cursors”.
Deallocate the prepared statement.
If a cursor is declared on the statement, first close and deallocate the cursor:
ct_cursor(CS_CURSOR_CLOSE, CS_DEALLOC) or, if the cursor is not open, ct_cursor(CS_CURSOR_DEALLOC)
ct_send
ct_results, in a loop
Initiate and send a command to deallocate the prepared statement:
ct_dynamic(CS_DEALLOC)
ct_send
ct_results, in a loop
The deallocate command returns no fetchable results.
Each step in the process above sends one dynamic SQL command to the server. After sending each command, the application must handle the results with ct_results. Code your application to handle the results of a dynamic SQL command with a standard results loop, as discussed in “Structure of the basic loop”.