How to process results (ODBC)

If bound columns are used, for each column you want to bind to, call SQLBindCol to bind a program buffer to the column.

For each row in the result set:

Call SQLFetch to get the next row.

If bound columns are used, use the data now available in the bound column buffers.

If unbound columns are used, call SQLGetData one or more times to get the data for unbound columns after the last bound column. Calls to SQLGetData should be in increasing order of column number.

Call SQLGetData multiple times to get data from a text or image column.

When SQLFetch signals the end of the result set by returning SQL_NO_DATA, call SQLMoreResults to determine if another result set is available.

If it returns SQL_SUCCESS, another result set is available.

If it returns SQL_NO_DATA, no more result sets are available.

If it returns SQL_SUCCESS_WITH_INFO or SQL_ERROR, call SQLGetDiagRec to determine if the output from a PRINT or RAISERROR statement is available.

If bound statement parameters are used for output parameters or the return value of a stored procedure, use the data now available in the bound parameter buffers. Also, when bound parameters are used, each call to SQLExecute or SQLExecDirect will have executed the SQL statement S times, where S is the number of elements in the array of bound parameters. This means that there will be S sets of results to process, where each set of results comprises all of the result sets, output parameters, and return codes usually returned by a single execution of the SQL statement.

Note that when a result set contains compute rows, each compute row is made available as a separate result set. These compute result sets are interspersed within the normal rows and break normal rows into multiple result sets.

Optionally, call SQLFreeStmt with an fOption of SQL_UNBIND to release any bound column buffers.

If another result set is available, go to Step 1.

To cancel processing a result set before SQLFetch returns SQL_NO_DATA, call SQLCloseCursor.

Examples

The example shows how to use either SQLBindCol or SQLGetData. It has been simplified by removing all error checking. The program can be compiled with either the SQLBindCol function or the SQLGetData function commented out, the resulting executable operates the same.

The complete sample code is in this file: SQLBindColOrSQLGetData.cpp. You can download a file containing this sample from the Microsoft® SQL Server™ Downloads page at this Microsoft Web site.

This sample was developed for ODBC version 3.0 or later. It was developed with Microsoft Visual C++® version 6.0, and may expose properties of the Microsoft Foundation Classes.