Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

How to use a statement (ODBC)

SQL Server 2000

  New Information - SQL Server 2000 SP3.

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

Allocating a Statement Handle

Constructing an SQL Statement

Direct Execution

Freeing a Statement Handle

Prepared Execution

SQLBindParameter

SQLDescribeParam

SQLFreeHandle

SQLGetData

SQLGetStmtAttr

SQLMoreResults

SQLRowCount

SQLSetStmtAttr

Show:
© 2015 Microsoft