Fetch
Retrieve a specific line from the Transact-SQL server cursor.
grammar
FETCH [[Next | PRIOR | First | Last | Absolute {n | @nvar} | relative {n | @nvar}] from] {{[global] cursor_name} | @cursor_variable_name} [@variable_name [, ... n ]]]
parameter
NEXT
Returns the result line immediately after the current row, and the current row is incremented to the result line. If Fetch next is the first extraction operation of the cursor, the first line of the result is returned. Next is the default cursor extraction option.
PRIOR
Returns the result line in front of the current line, and the current row is decremented to the result line. If the Fetch Prior is the first extraction operation of the cursor, there is no row to return and the cursor is placed before the first line.
First
Returns the first line in the cursor and use it as the current line.
Last
Returns the last row in the cursor and use it as the current line.
Absolute {n | @nvar}
If n or @nvar is a positive number, return the Nth line starting from the cursor head and turn the returned row into a new current line. If n or @nvar is a negative number, return the Nth line before the tail, turn the returned row into a new current line. If n or @nvar is 0, there is no row to return. n must be integer and @nvar must be Smallint, Tinyint or int.
Relative {n | @nvar}
If n or @nvar is a positive number, return the Nth line after the current row and turn the returned row into a new current line. If n or @nvar is a negative number, return the Nth line before the current row and turn the returned row into a new current line. If n or @nvar is 0, return to the current row. If the FETCH Relative N or @nvar is specified as a negative or 0 when the cursor is first extracted, there is no row return. n must be integer and @nvar must be Smallint, Tinyint or int.
Global
Specifies Cursor_name to refer to a global cursor.
Cursor_name
The name of the open cursor is to be extracted from it. If there is a global and partial cursor as a name of Cursor_name, if it is specified as Global, Cursor_Name corresponds to a global cursor, and does not specify that Global corresponds to a local cursor.
@cursor_variable_name
Cursor variable name, reference to the open cursor to be subjected to extraction operation.
INTO @variable_name [, ... n]
Allows the column data of the extracted operation to be placed in a local variable. Each variable in the list is associated with the corresponding column of the cursor results set from left to right. The data type of each variable must match the data type of the result column or the implicit conversion supported by the result column data type. The number of variables must be consistent with the number of columns in the cursor selection list.