交易表定义:
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
答案 0 :(得分:2)
打开游标后,你必须使用FETCH .. like下面的
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @name
因为你没有这样做,Fetch_status将为-1
根据相关更改进行了更新:
由于此WHERE Value>100
没有结果集,因此您获得-1。当您使用> = 100时,会有结果集,您将看不到提取状态-1
< / p>