游标提取失败.@@FETCH_STATUS为 -1



事务表定义:

create table Transactions ([ID] [int] NOT NULL, [Value] [int] NOT NULL)

让我们插入一些记录。

INSERT INTO Transactions Values(1,100)
INSERT INTO Transactions Values(2,10)

这就是我使用光标的方式

create table #Tmp_Transactions ([ID] [int] NOT NULL, [Value] [int] NOT NULL)
INSERT INTO #Tmp_Transactions SELECT * FROM Transactions WHERE Value>100
DECLARE @rowcount int
SET @rowcount = @@rowcount
PRINT @rowcount
DECLARE @ID int
DECLARE txcursor CURSOR FOR SELECT ID FROM #Tmp_Transactions
OPEN txcursor
FETCH NEXT FROM txcursor INTO @ID
PRINT @@FETCH_STATUS ---//prints -1
CLOSE txcursor
DEALLOCATE txcursor
drop table #Tmp_Transactions

-----指纹

(0 row(s) affected)
0
-1

打开光标后,你必须使用FETCH。像下面一样

OPEN db_cursor  
FETCH NEXT FROM db_cursor INTO @name  

由于您不这样做,Fetch_status将是 -1

根据相关更改进行更新:

由于这个WHERE Value>100没有结果集,所以你得到-1.当你使用>=100时,有一个结果集,你不会看到获取状态-1

最新更新