CREATE OR ALTER PROCEDURE StudentsToGroups AS BEGIN DECLARE @num VARCHAR(15) DECLARE stud_cursor CURSOR FOR SELECT id FROM stud ORDER BY surStud, nameStud OPEN stud_cursor FETCH NEXT FROM stud_cursor INTO @num WHILE @@FETCH_STATUS = 0 BEGIN BEGIN TRY EXEC @status = dbo.AddStudent @num END TRY BEGIN CATCH IF (ERROR_NUMBER() = 2 OR ERROR_NUMBER() = 3) CONTINUE; ELSE IF ERROR_NUMBER() = 4 THROW 5, 'Unsucessful', 1; ELSE THROW; END CATCH FETCH NEXT FROM stud_cursor INTO @num END CLOSE stud_cursor DEALLOCATE stud_cursor END GO EXEC studentsToGroups Something in my code is causing infinite loop. I really don't get it why because I use FETCH NEXT in While Loop. Also, the procedure is doing it's job, it fills all the students in groups, but it simply never ends.