I need some examples for @@fetch_status
values -2
and -9
0 = The FETCH statement was successful.
-1 = The FETCH statement failed or the row was beyond the result set.
Here is a sample cursor example
declare @country varchar(50)
declare cur_country cursor for
select name from global
open cur_country
fetch next from cur_country into @country
print @@FETCH_STATUS
while (@@FETCH_STATUS=0)
begin
insert into country select @country
fetch next from cur_country into @country
end
close cur_country
deallocate cur_country.
-2
The row fetched is missing.
-9
The cursor is not performing a fetch operation.
basically need scenario where @@FETCH_STATUS
gives -2
0r -9
@@FETCH_STATUS = -2
usually happens when some process OUTSIDE of the cursor deletes a row in the table the cursor is based on.If job 1 opens a cursor and starts looping through records in table1, and while Job 1 is looping, Job 2 comes along and deletes certain records in table1, job 1 might return a -2 when it tries to retrieve a row it was expecting to find (because it was there when the cursor started).
The following Topic contains an example of
@@FETCH_STATUS = -2
While researching i found the following explanation from this Topic :
And like DVT commented. This Stackoverflow question contains an example on
@@FETCH_STATUS = -9
More info about
@@FETCH_STATUS
can be found at this MSDN articleUpdate
Two months ago, I published an article about @@FETCH_STATUS where I provided some examples that produce -2 and -9 values (The other answer helped me a lot while writing this article):