Scrolling and Fetching Rows


System_CAPS_ICON_warning.jpg Warning

SQL Server Native Client (SNAC) is not supported beyond SQL Server 2012. Avoid using SNAC in new development work, and plan to modify applications that currently use it. The Microsoft ODBC Driver for SQL Server provides native connectivity from Windows to Microsoft SQL Server and Microsoft Azure SQL Database.

To use a scrollable cursor, an ODBC application must:

  • Set the cursor capabilities using SQLSetStmtAttr.

  • Open the cursor using SQLExecute or SQLExecDirect.

  • Scroll and fetch rows using SQLFetch or SQLFetchScroll.

Both SQLFetch and SQLFetchSroll can fetch blocks of rows at a time. The number of rows returned is specified by using SQLSetStmtAttr to set the SQL_ATTR_ROW_ARRAY_SIZE parameter.

ODBC applications can use SQLFetch to fetch through a forward-only cursor.

SQLFetchScroll is used to scroll around a cursor. SQLFetchScroll supports fetching the next, prior, first, and last rowsets in addition to relative fetching (fetch the rowset n rows from the start of the current rowset) and absolute fetching (fetch the rowset starting at row n). If n is negative in an absolute fetch, rows are counted from the end of the result set. An absolute fetch of row -1 means to fetch the rowset that starts with the last row in the result set.

Applications that use SQLFetchScroll only for its block cursor capabilities, such as reports, are likely to pass through the result set a single time, using only the option to fetch the next rowset. Screen-based applications, on the other hand, can take advantage of all the capabilities of SQLFetchScroll. If the application sets the rowset size to the number of rows displayed on the screen and binds the screen buffers to the result set, it can translate scroll bar operations directly to calls to SQLFetchScroll.

Scroll bar operationSQLFetchScroll scrolling option
Line upSQL_FETCH_RELATIVE with FetchOffset equal to -1
Line downSQL_FETCH_RELATIVE with FetchOffset equal to 1
Scroll box to topSQL_FETCH_FIRST
Scroll box to bottomSQL_FETCH_LAST
Random scroll box positionSQL_FETCH_ABSOLUTE

Using Cursors (ODBC)

Community Additions