@@ fetch_status
Returns the status of the last cursor executed by the FETCH statement, rather than any cursor that is currently connected.
The return value describes the 0FETCH statement success. -1fetch statement failed or this line is not in the result. -2 The extracted line does not exist.
grammar
@@ fetch_status
Return type
Integer
Comment
Because @@ fetch_status is global for all cursors on a connection, be careful to use @@ fetch_status. After executing a FETCH statement, @@ fetch_status must be tested before another FETCH statement is performed on another cursor. The value of @@ fetch_status is not defined before any extraction operation appears.
For example, the user performs a FETCH statement from a cursor and then calls a stored procedure, which opens and processes the results of another cursor. When control returns from the called stored procedure, @@ fetch_status reflects the result of the last FETCH statement executed during the stored procedure, not the result of the FETCH statement before the stored procedure is called.
Example
The following example is controlled with @@ fetch_status in a cursor activity in a While loop.
Declare Employee_Cursor Cursor for
Select lastname, firstname from northwind.dbo.employees
Open Employee_Cursor
Fetch next from Employee_cursor
While @@ fetch_status = 0
Begin
Fetch next from Employee_cursor
End
Close Employee_cursor
Deallocate Employee_cursor