Cursor Concurrency (Database Engine)
Microsoft SQL Server supports four concurrency options for server cursors:
READ_ONLY
OPTIMISTIC WITH VALUES
OPTIMISTIC WITH ROW VERSIONING
SCROLL LOCKS
These cursor concurrency options may generate scroll locks, depending on the locking hints specified in the SELECT statement in the cursor definition. Scroll locks are acquired on each row in a fetch and held until the next fetch or the close of the cursor, whichever occurs first. On the next fetch, the server acquires scroll locks for the rows in the new fetch and then releases the scroll locks for the rows in the previous fetch. Scroll locks are independent of transaction locks and may persist past a commit or rollback operation. If the option to close cursors on commit is off, a COMMIT does not close any open cursors and scroll locks are preserved past the commit to maintain the isolation of the fetched data.
The type of scroll locks acquired depends on the cursor concurrency option and the locking hints in the cursor SELECT statement.
Note |
|---|
Scroll locks are supported only for keyset-driven and dynamic cursors. |
Locking hints | Read only | Optimistic with values | Optimistic with row versioning | Locking |
|---|---|---|---|---|
No Hints | - | - | - | Update |
NOLOCK* | - | - | - | - |
HOLDLOCK | - | - | - | Update |
UPDLOCK | - | - | - | Update |
TABLOCKX | - | - | - | Update |
All Others | - | - | - | Update |
*Specifying the NOLOCK hint makes the table on which it is specified read-only through the cursor.
The concurrency options are specified differently in each cursor environment:
Transact-SQL cursors
Specify the READ_ONLY, SCROLL_LOCK, and OPTIMISTIC keywords on the DECLARE CURSOR statement. The OPTIMISTIC keyword specifies optimistic with row versioning, Transact-SQL cursors do not support the optimistic with values concurrency option.
ADO applications
Specify adLockReadOnly, adLockPessimistic, adLockOptimistic, or adLockBatchOptimistic in the LockType property of a Recordset object.
ODBC applications
Set the statement attribute SQL_ATTR_CONCURRENCY to SQL_CONCUR_READ_ONLY, SQL_CONCUR_ROWVER, SQL_CONCUR_VALUES, or SQL_CONCUR_LOCK.
Note