There are two types of SQL server cursors. One type, discussed here, is based on the ANSI syntax for cursors and is intended for triggers and stored procedures. The second type of SQL Server cursor is provided by the DB-Library, the SQL Server ODBC driver, and the SQL Server OLE DB provider.
For More Information For more information on the types of cursors available in the DB-Library, see DB-Library Cursors in this chapter.
SQL Server can provide a forward-scrolling, read-only cursor. This is sometimes called the "firehose" cursor because it is very efficient at retrieving data and does so by keeping the connection open.
A SQL Server cursor is defined by specifying one or more cursor characteristics in the DECLARE statement. The cursor characteristics available in the SQL cursor library are listed in the following table.
Cursor type | Constant |
Static cursor. Defines a cursor that makes a copy of the data. Changes to base tables are not detected. | INSENSITIVE |
Forward and backward scrolling. Changes made to the base tables are available in subsequent fetches. | SCROLL |
Read only. Prevents updates from occurring on any row in this result set. | FOR READ-ONLY |
Updatable. Defines updatable columns. All columns are updatable unless the cursor has also been defined as READ-ONLY. | FOR UPDATE |
SQL Server does not use a server cursor for any of the following situations:
To handle concurrency issues, SQL Server automatically chooses a suitable lock type depending on what type of Transact-SQL operation your application is performing. SQL Server can place locks at the page, table, extent, and database level. You can additionally assign the HOLDLOCK attribute in the SELECT statement to force SQL Server to hold its locks until the transaction is complete.
The types of possible locks are:
For More Information For more information on the SQL Server cursor library, search online for "Server Cursors" in MSDN Library Visual Studio 6.0 and "DECLARE Statement" in SQL Server Books Online. For more information on using locks to handle multiuser concurrency situations, see Managing Concurrency with Cursor Locks.