交易表定義:遊標FETCH失敗。 @@ 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
您**還**需要用'INSERT INTO #Tmp_Transactions SELECT * FROM Transactions等'代替'SELECT * INTO'' –
無效。它仍然會返回-1 – user1
您在#Tmp_Transactions中有多少行?你有任何行嗎?根據[Microsoft的文檔](https://docs.microsoft.com/en-us/sql/t-sql/functions/fetch-status-transact-sql#return-value),狀態「-1」表示「」 FETCH語句失敗或行超出結果集。「' –