Using Cursors (ODBC)


ODBC supports a cursor model that allows:

  • Several types of cursors.

  • Scrolling and positioning within a cursor.

  • Several concurrency options.

  • Positioned updates.

ODBC applications rarely declare and open cursors or use any cursor-related Transact-SQL statements. ODBC automatically opens a cursor for every result set returned from an SQL statement. The characteristics of the cursors are controlled by statement attributes set with SQLSetStmtAttr before the SQL statement is executed. The ODBC API functions for processing result sets support the full range of cursor functionality, including fetching, scrolling, and positioned updates.

This is a comparison of how Transact-SQL scripts and ODBC applications work with cursors.

Define cursor behaviorSpecify through DECLARE CURSOR parametersSet cursor attributes by using SQLSetStmtAttr
Open a cursorDECLARE CURSOR OPEN cursor_nameSQLExecDirect or SQLExecute
Fetch rowsFETCHSQLFetch or SQLFetchScroll
Positioned updateWHERE CURRENT OF clause on UPDATE or DELETESQLSetPos
Close a cursorCLOSE cursor_name DEALLOCATESQLCloseCursor

The server cursors implemented in SQL Server support the functionality of the ODBC cursor model. The SQL Server Native Client driver uses server cursors to support the cursor functionality of the ODBC API.

SQL Server Native Client (ODBC)
CLOSE (Transact-SQL)
FETCH (Transact-SQL)
OPEN (Transact-SQL)