Retrieving Data Through Cursors
There are two ways to retrieve data through cursors:
-
You can bind columns of the results set to storage locations by using SQLBindCol, and then use SQLFetch to move to the next row in the results set. For bulk fetches, you could bind columns to an array and use SQLExtendedFetch. The SQL Server driver supports row-wise as well as column-wise binding. Keep in mind that when using server cursors, each SQLFetch or SQLExtendedFetch operation involves a round trip to the server and the connection remains free between these operations. The server tracks all required state information (for example, current cursor position). Also, cursor operations do not have to be sequential. The server allows you to traverse the cursor in the forward or backward directions relative to the current position. You can also directly set the position to an absolute row number (except for Dynamic cursors) in the results set.
-
You can retrieve data for unbound columns (columns for which storage has not been allocated). Use SQLFetch or SQLExtendedFetch to position the cursor on the next row, and call SQLGetData to retrieve data for specific unbound columns. You can call SQLGetData multiple times on the same column to incrementally retrieve data. This is useful if you are retrieving large amounts of data from a text or image column. Keep in mind, however, that the connection is busy until you read all the data associated with the column. If you issue an SQLSetPos with the Position option, the SQL Server driver flushes the remaining data and frees up the connection, thereby allowing you to use it for processing on another hstmt.
You can retrieve data from both bound and unbound columns in the same row.
When retrieving rows involving potentially large text and image columns, if you do not want to access the text data column in the row, do not bind that column. When you do an SQLExtendedFetch, the retrieved row will only contain the text pointer for the unbound column as opposed to the previous behavior of returning the complete row including the text data.
The following ODBC functions are relevant: