sp_cursor (Transact-SQL)
Requests positioned updates. This procedure performs operations on one or more rows within a cursor's fetch buffer. sp_cursor is invoked by specifying ID = 1 in a tabular data stream (TDS) packet.
sp_cursor cursor, optype, rownum, table [ , value[...n]]]
optype Parameter
With the exception of the combinations of SETPOSITION with UPDATE, DELETE, REFRESH, or LOCK; or ABSOLUTE with either UPDATE or DELETE, the optype values are mutually exclusive.
The SET clause of the UPDATE value is constructed from the value parameter.
One benefit of using the INSERT optype value is that you can avoid converting non-character data into character format for inserts. The values are specified in the same way as UPDATE. If any required columns are not included, the INSERT fails.
The SETPOSITION value does not affect the starting point of any RELATIVE, NEXT, or PREVIOUS fetch operation, nor do any updates or deletes performed using the sp_cursor interface. Any number that does not specify a row in the fetch buffer will result in the position being set to 1 with no error being returned. Once SETPOSITION is executed, the position remains in effect until the next sp_cursorfetch operation, T-SQL FETCH operation, or sp_cursor SETPOSITION operation through the same cursor. A subsequent sp_cursorfetch operation will set the position of the cursor to the first row in the new fetch buffer while other cursor calls will not affect the value of the position. SETPOSITION can be linked by an OR clause with REFRESH, UPDATE, DELETE, or LOCK in order to set the value of the position to the last modified row.
If a row in the fetch buffer is not specified through the rownum parameter, the position will be set to 1, with no error returned. Once the position is set, it remains in effect until the next sp_cursorfetch operation, T-SQL FETCH operation, or sp_cursor SETPOSITION operation is performed on the same cursor.
SETPOSITION be linked by an OR clause with REFRESH, UPDATE, DELETE, or LOCK to set the cursor position to the last modified row.
rownum Parameter
If specified, the rownum parameter can be interpreted as the row number within the keyset instead of the row number within the fetch buffer. The user is responsible for ensuring that concurrency control is maintained. This means that for SCROLL_LOCKS cursors, you must independently maintain a lock on the given row (this can be done through a transaction). For OPTIMISTIC cursors, you must have previously fetched the row to perform this operation.
table Parameter
If the optype value is UPDATE or INSERT and a full update or insert statement is submitted as the value parameter, the value specified for table is ignored.
Note |
|---|
Pertaining to views, only one table participating in the view may be modified. The value parameter column names must reflect the column names in the view, but the table name can be that of the underlying base table (in which case sp_cursor will substitute the view name). |
value Parameter
There are two alternatives to the rules for using value as stated earlier in the Arguments section:
You can use a name that is '@' pre-pended to the name of the column in the select-list for any named value parameters. One advantage of this alternative is that data conversion may not be necessary.
Use a parameter to either submit a complete UPDATE or INSERT statement or use multiple parameters to submit portions of an UPDATE or INSERT statement which SQL Server will then build into a complete statement. Examples of this can be found in the Examples section later in this topic.
Alternative value Parameter Uses
For UPDATE:
When a single parameter is used, an UPDATE statement may be submitted using the following syntax:
[ [ UPDATE <table name> ] SET ] {<column name> = expression} [,…n]
Note |
|---|
If UPDATE <table name> is specified, any value specified for the table parameter will be ignored. |
When multiple parameters are used, the first parameter must be a string in the following form:
[ SET ] <column name> = expression [,...n]
and the subsequent parameters must be in the form of:
<column name> = expression [,...n]
In this case, the <table name> in the constructed update statement is the one either specified or defaulted to by the table parameter.
For INSERT:
When a single parameter is used, an INSERT statement may be submitted using the following syntax:
[ [ INSERT [INTO] <table name> ] VALUES ] ( <expression> [,...n] )
Note |
|---|
If INSERT <table name> is specified, any value specified for the table parameter will be ignored. |
When multiple parameters are used, the first parameter must be a string in the following form:
[ VALUES ( ] <expression> [,...n]
and the subsequent parameters must be in the form of:
expression [,...n]
except where VALUES was specified, in which case there must be a trailing ")" after the last expression. In this case, the <table name> in the constructed UDPATE statement is the one either specified or defaulted to by the table parameter.
Note |
|---|
It is possible to submit one parameter as a named parameter, i.e. "@VALUES". In this case no other named parameters may be used. |

