Delen via


How to: Use a Statement (ODBC)

To use a statement

  1. Call SQLAllocHandle with a HandleType of SQL_HANDLE_STMT to allocate a statement handle.

  2. Optionally, call SQLSetStmtAttr to set statement options or SQLGetStmtAttr to get statement attributes.

    To use server cursors, you must set cursor attributes to values other than their defaults.

  3. Optionally, if the statement will be executed several times, prepare the statement for execution with SQLPrepare.

  4. Optionally, if the statement has bound parameter markers, bind the parameter markers to program variables by using SQLBindParameter. If the statement was prepared, you can call SQLNumParams and SQLDescribeParam to find the number and characteristics of the parameters.

  5. Execute a statement directly by using SQLExecDirect.

    - or -

    If the statement was prepared, execute it multiple times by using SQLExecute.

    - or -

    Call a catalog function, which returns results.

  6. Process the results by binding the result set columns to program variables, by moving data from the result set columns to program variables by using SQLGetData, or a combination of the two methods.

    Fetch through the result set of a statement one row at a time.

    - or -

    Fetch through the result set several rows at a time by using a block cursor.

    - or -

    Call SQLRowCount to determine the number of rows affected by an INSERT, UPDATE, or DELETE statement.

    If the SQL statement can have multiple result sets, call SQLMoreResults at the end of each result set to see if there are additional result sets to process.

  7. After results are processed, the following actions may be necessary to make the statement handle available to execute a new statement:

    • If you did not call SQLMoreResults until it returned SQL_NO_DATA, call SQLCloseCursor to close the cursor.

    • If you bound parameter markers to program variables, call SQLFreeStmt with Option set to SQL_RESET_PARAMS to free the bound parameters.

    • If you bound result set columns to program variables, call SQLFreeStmt with Option set to SQL_UNBIND to free the bound columns.

    • To reuse the statement handle, go to Step 2.

  8. Call SQLFreeHandle with a HandleType of SQL_HANDLE_STMT to free the statement handle.

See Also

Other Resources