Export (0) Print
Expand All

sp_cursoropen (Transact-SQL)


Opens a cursor. sp_cursoropen defines the SQL statement associated with the cursor and cursor options, and then populates the cursor. sp_cursoropenis equivalent to the combination of the Transact-SQL statements DECLARE_CURSOR and OPEN. This procedure is invoked by specifying ID =2 in a tabular data stream (TDS) packet.

Applies to: SQL Server (SQL Server 2008 through current version).

Topic link icon Transact-SQL Syntax Conventions

sp_cursoropen cursor OUTPUT, stmt  
    [, scrollopt[ OUTPUT ] [ , ccopt[ OUTPUT ]  
    [ ,rowcount OUTPUT [ ,boundparam][,...n]]] ]]  

A SQL Server-generated cursor identifier. cursor is a handle value that must be supplied on all subsequent procedures involving the cursor, such as sp_cursorfetch. cursor is a required parameter with an int return value.

cursor allows multiple cursors to be active on a single database connection.

Is a required parameter that defines the cursor result set. Any valid query string (syntax and binding) of any string type (regardless of Unicode, size, etc.) can serve as a valid stmt value type.

Scroll option. scrollopt is an optional parameter that requires one of the following int input values.


Because of the possibility that the requested value is not appropriate for the cursor defined by stmt, this parameter serves as both input and output. In such cases, SQL Server assigns an appropriate value.

Concurrency control option. ccopt is an optional parameter that requires one of the following int input values.

0x0002SCROLL_LOCKS (previously known as LOCKCC)
0x0004OPTIMISTIC (previously known as OPTCC)
0x0008OPTIMISTIC (previously known as OPTCCVAL)

As with scrollopt, SQL Server can override the requested ccopt values.

The number of fetch buffer rows to use with AUTO_FETCH. The default is 20 rows. rowcount behaves differently when assigned as an input value versus a return value.

As input valueAs return value
When the AUTO_FETCH scrollopt value is specified rowcount represents the number of rows to place into the fetch buffer.

Note: >0 is a valid value when AUTO_FETCH is specified, but is otherwise ignored.
Represents the number of rows in the result set, except when the scrollopt AUTO_FETCH value is specified.

Signifies the use of additional parameters. boundparam is an optional parameter that should be specified if the scrollopt PARAMETERIZED_STMT value is set to ON.

If no error is raised, sp_cursoropen returns one of the following values.

The procedure executed successfully.

An error occurred during the execution (a minor error, not severe enough to raise an error in the operation).

An asynchronous operation is in progress.

A FETCH operation is in process.

This cursor has been deallocated by SQL Server and is unavailable.

When an error is raised, the return values may be inconsistent and the accuracy cannot be guaranteed.

When the rowcount parameter is specified as a return value, the following result set occurs.

Returned if the number of rows is unknown or not applicable.

Returned when an asynchronous population is in effect. Represents the number of rows that were placed into the fetch buffer when the scrollopt AUTO_FETCH value is specified.

If RPC is in use, the return values are as follows.

Procedure is successful.

Procedure failed.

A keyset cursor is being asynchronously generated.

A fast-forward cursor has been automatically closed.

System_CAPS_ICON_note.jpg Note

If the sp_cursoropen procedure executes successfully, the RPC return parameters and a result set with TDS column format information (0xa0 & 0xa1 messages) are sent. If unsuccessful, one or more TDS error messages are sent. In either case, no row data will be returned and the done message count will be zero. If you are using a version of SQL Server earlier than 7.0, 0xa0, 0xa1 (standard for SELECT statements) are returned along with 0xa5 and 0xa4 token streams. If you are using SQL Server 7.0, 0x81 is returned (standard for SELECT statements) along with the 0xa5 and 0xa4 token streams.

If stmt specifies the execution of a stored procedure, the input parameters may either be defined as constants as part of the stmt string, or specified as boundparam arguments. Declared variables can be passed as bound parameters in this way.

The allowed contents of the stmt parameter depend upon whether or not the ccopt ALLOW_DIRECT return value has been linked by OR to the rest of the ccopt values, i.e.:

  • If ALLOW_DIRECT is not specified, either a Transact-SQL SELECT or EXECUTE statement calling for a stored procedure containing a single SELECT statement must be used. Furthermore, the SELECT statement must qualify as a cursor; that is, it cannot contain the keywords SELECT INTO or FOR BROWSE.

  • If ALLOW_DIRECT is specified, this may result in one or more Transact-SQL statements, including those that, in turn, execute other stored procedures with multiple statements. Non-SELECT statements or any SELECT statement that contains the keywords SELECT INTO or FOR BROWSE will simply be executed and will not result in the creation of a cursor. The same is true for any SELECT statement included in a batch of multiple statements. In cases where a SELECT statement contains clauses that pertain only to cursors, those clauses are ignored. For instance, when the value of ccopt is 0x2002, this is a request for:

    • A cursor with scroll locks, if there is only a single SELECT statement that qualifies as a cursor, or

    • A direct statement execution if there are multiple statements, a single non-SELECT statement, or a SELECT statement that does not qualify as a cursor.

The first five scrollopt values (KEYSEY, DYNAMIC, FORWARD_ONLY, STATIC, and FAST_FORWARD) are mutually exclusive.

PARAMETERIZED_STMT and CHECK_ACCEPTED_TYPES can be linked by OR to any of the first five values.

AUTO_FETCH and AUTO_CLOSE can be linked by OR to FAST_FORWARD.


STATIC cursors are always opened as READ_ONLY. This means that the underlying table cannot be updated through this cursor.

The first four ccopt values (READ_ONLY, SCROLL_LOCKS, and both OPTIMISTIC values) are mutually exclusive.

System_CAPS_ICON_note.jpg Note

Choosing one of the first four ccopt values dictates whether the cursor is read-only, or if locking or optimistic methods are used to prevent lost updates. If a ccopt value is not specified, the default value is OPTIMISTIC.

ALLOW_DIRECT and CHECK_ACCEPTED_TYPES can be linked by OR to any of the first four values.

UPDT_IN_PLACE can be linked by OR to READ_ONLY, SCROLL_LOCKS, or either of the OPTIMISTIC values.

If CHECK_ACCEPTED_TYPES is ON, at least one of the last four ccopt values (READ_ONLY_ACCEPTABLE, SCROLL_LOCKS_ACCEPTABLE, and either of the OPTIMISTIC_ACCEPTABLE values) must also be ON.

Positioned UPDATE and DELETE functions may be performed only within the fetch buffer and only if the ccopt value equals SCROLL_LOCKS or OPTIMISTIC. If SCROLL_LOCKS is the specified value, the operation is guaranteed to succeed. If OPTIMISTIC is the specified value, the operation will fail if the row has changed since it was last fetched.

The reason for this failure is that when OPTIMISTIC is the specified value, an optimistic currency control function is performed by comparing timestamps or checksum values, as determined by SQL Server. If any of these rows do not match, the operation will fail.

Specifying UPDT_IN_PLACE as the return value governs the following results:

  • If not set when performing a positioned update on a table with a unique index, the cursor deletes the row from its work table and inserts it at the end of any of the key columns used by the cursor, thereby changing those columns.

  • If set ON, the cursor will simply update the key columns in the work table's original row.

The parameter name should be paramdef when PARAMETERIZED_STMT is specified, according to the error message in the code. When PARAMETERIZED_STMT is not specified, no name is specified in the error message.

The RPC RETURN_METADATA input flag can be set to 0x0001 to request that cursor select list metadata be returned in the TDS stream.

bound_param Parameter

Any parameters after the fifth are passed along to the statement plan as input parameters. The first such parameter must be a string in the form of:

{ local variable name data type } [,…n]

Subsequent parameters are used to pass the values to be substituted for the local variable name in the statement.

sp_cursorfetch (Transact-SQL)
System Stored Procedures (Transact-SQL)

Community Additions

© 2016 Microsoft