

A static cursor always displays the result set as it was when the cursor was opened. The complete result set of a static cursor is built in tempdb when the cursor is opened. When a database API cursor attribute or property is set to forward-only, SQL Server implements this as a forward-only dynamic cursor. The database API cursor models assume that static, keyset-driven, and dynamic cursors are always scrollable. Transact-SQL cursors support forward-only static, keyset-driven, and dynamic cursors. SQL Server considers both forward-only and scroll as options that can be applied to static, keyset-driven, and dynamic cursors. In cases where a value used to determine the location of the row within the result set is modified, such as updating a column covered by a clustered index, the modified value is visible through the cursor.Īlthough the database API cursor models consider a forward-only cursor to be a distinct type of cursor, SQL Server does not. The effects of all INSERT, UPDATE, and DELETE statements made by the current user or committed by other users that affect rows in the result set are visible as the rows are fetched from the cursor.īecause the cursor cannot be scrolled backward, most changes made to rows in the database after the row was fetched are not visible through the cursor. The rows are not retrieved from the database until they are fetched. These are also called firehose cursors and support only fetching the rows serially from the start to the end of the cursor. Forward-onlyĪ forward-only cursor is specified as FORWARD_ONLY and READ_ONLY and does not support scrolling. For more information, see worktables in the Query Processing Architecture Guide.

STATIC cursors use worktables from its inception. Just like aggregation or sort operations that spill, these incur in I/O, and are a potential performance bottleneck. Type of CursorsĬursors may leverage tempdb worktables.
#Cursor in sql driver#
Each time a client application calls an API cursor function, the SQL Server Native Client ODBC driver or the ADO DLL performs the cursor operation on the result set rows cached on the client. Client cursors are implemented by caching all the result set rows on the client. Client cursorsĬlient cursors are implemented internally by the SQL Server Native Client ODBC driver and by the DLL that implements the ADO API. Each time a client application calls an API cursor function, the SQL Server Native Client OLE DB provider or ODBC driver transmits the request to the server for action against the API server cursor.


API server cursors are implemented on the server. Application programming interface (API) server cursorsĪPI cursors support the API cursor functions in OLE DB and ODBC. They may also be contained in batches, stored procedures, or triggers. Transact-SQL cursors are implemented on the server and are managed by Transact-SQL statements sent from the client to the server. Transact-SQL cursors are based on the DECLARE CURSOR syntax and used mainly in Transact-SQL scripts, stored procedures, and triggers. SQL Server supports three cursor implementations.
#Cursor in sql update#
When that is the case, if there is no requirement to update tables based on the cursor, then use firehose cursors, meaning fast-forward and read-only cursors. However, there are scenarios where cursors are not only unavoidable, they are actually needed. In some scenarios, if there is a primary key on a table, a WHILE loop can be used instead of a cursor, without incurring in the overhead of a cursor. Providing Transact-SQL statements in scripts, stored procedures, and triggers access to the data in a result set. Supporting different levels of visibility to changes made by other users to the database data that is presented in the result set. Supporting data modifications to the rows at the current position in the result set. Retrieving one row or block of rows from the current position in the result set. Cursors are an extension to result sets that provide that mechanism.Īllowing positioning at specific rows of the result set. These applications need a mechanism to work with one row or a small block of rows at a time. Applications, especially interactive online applications, cannot always work effectively with the entire result set as a unit. This complete set of rows returned by the statement is known as the result set. For example, the set of rows returned by a SELECT statement consists of all the rows that satisfy the conditions in the WHERE clause of the statement. Operations in a relational database act on a complete set of rows. Applies to: SQL Server (all supported versions) Azure SQL Database
