Using Cursors

ODBC and SQL Server

ODBC and SQL Server

Using Cursors

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.

Action Transact-SQL ODBC
Define cursor behavior Specify through DECLARE CURSOR parameters Set cursor attributes by using SQLSetStmtAttr
Open a cursor DECLARE CURSOR
OPEN cursor_name
SQLExecDirect or SQLExecute
Fetch rows FETCH SQLFetch or SQLFetchScroll
Positioned update WHERE CURRENT OF clause on UPDATE or DELETE SQLSetPos
Close a cursor CLOSE cursor_name
DEALLOCATE
SQLCloseCursor

The server cursors implemented in Microsoft® SQL Server™ support the functionality of the ODBC cursor model. The SQL Server ODBC driver uses server cursors to support the cursor functionality of the ODBC API.

See Also

CLOSE

Cursors

DEALLOCATE

DECLARE CURSOR

FETCH

OPEN

SQLCloseCursor

SQLFetchScroll

SQLSetStmtAttr