1
我對TSQL遊標中的無限循環有個奇怪的問題。當我不將TOP 300添加到遊標的定義選擇語句時,遊標會無限循環。以下是代碼的示例:對此問題的任何幫助非常感謝。在SELECT語句中沒有TOP xxx的TSQL遊標無限循環
DECLARE @Done BIT
SET @Done = 0
DECLARE cursOut CURSOR LOCAL FAST_FORWARD
FOR
SELECT
--TOP 300
FirstName FirstName
,LastName LastName
,MiddleName MiddleName
,Email Email
,Address1 Address1
,Address2 Address2
,City City
,[State] [State]
FROM StagedUsers
OPEN cursOut;
WHILE (@Done = 0)
BEGIN
--Fetch next row
FETCH NEXT
FROM cursOut
INTO ,@v_FirstName
,@v_LastName
,@v_MiddleName
,@v_Email
,@v_Address1
,@v_Address2
,@v_City
,@v_State
IF (@@FETCH_STATUS <> 0)
BEGIN
SET @Done = 1
BREAK
END
--if @batch = 0
BEGIN TRANSACTION
--process statements
--updates or insert statements
--Commit transaction
COMMIT TRANSACTION
--End While
END
--CleanUp:
CLOSE cursOut
DEALLOCATE cursOut
感謝, Renegrin
您是否插入了您正在選擇的同一張表中? – Alex
不,我不更新或插入同一張表。 – Renegrin
你的遊標定義似乎缺少一個'FROM'子句。或者我在這裏錯過了什麼? – GolfWolf