title: TSQL Cursor Infinite Loop without TOP xxx in SELECT Statement
body: <p>I have a curious problem with an infinite loop in a TSQL cursor. The cursor loops infinitely when I do not add TOP 300 to the defining select statement of the cursor. The following is an example of the code: Any assistance to this issue is much appreciated.</p>
  
  <pre><code>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 &lt;&gt; 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
  </code></pre>
  
  <p>Thanks,
  Renegrin</p>