我对 TSQL 游标中的无限循环有一个奇怪的问题。当我没有将 TOP 300 添加到游标的定义选择语句时,游标会无限循环。以下是代码示例:非常感谢对此问题的任何帮助。
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
谢谢, 雷内格林