Program structure for the prepare-and-execute method

Most applications will use the steps below to prepare and execute a dynamic SQL statement:

  1. Prepare the dynamic SQL statement.

    • ct_dynamic(CS_PREPARE)

    • ct_send

    • ct_results, in a loop

    The prepare command returns no fetchable results.

  2. (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.

  3. (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.

  4. 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”.

  5. Deallocate the prepared statement.

    If a cursor is declared on the statement, first close and deallocate the cursor:

    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”.