Just as a shot in the dark, here is an update that is similar to what your cursor does. The update assumes some sort of order on the ReprintID.-- SetupDECLARE @ReprintInfo TABLE(JobID INT, ReprintID INT, ReprintCount INT)INSERT @ReprintInfoSELECT 1, 4, 0 UNION ALLSELECT 1, 5, 0 UNION ALLSELECT 1, 6, 0 UNION ALLSELECT 1, 7, 0 UNION ALLSELECT 1, 8, 0 UNION ALLSELECT 1, 1, 0 UNION ALLSELECT 1, 2, 0 UNION ALLSELECT 1, 3, 0 UNION ALLSELECT 2, 9, 0 UNION ALLSELECT 2, 10, 0 UNION ALLSELECT 2, 11, 0 UNION ALLSELECT 2, 12, 0 UNION ALLSELECT 2, 13, 0 UNION ALLSELECT 2, 14, 0 UNION ALLSELECT 2, 15, 0-- One Non-cursor wayUPDATE i SET ReprintCount = ( SELECT COUNT(*) FROM @ReprintInfo ri WHERE ri.ReprintID <= i.ReprintID AND ri.JobID = i.JobID )FROM @ReprintInfo iSELECT * FROM @reprintinfo-- Cursor wayDeclare @RpID int Declare @JobID intdeclare @ID intdeclare @cnt intset @cnt = 0set @ID = 0declare c1 cursor forselect ReprintID, JobID from @ReprintInfo order by JobIDopen c1fetch next from c1 into @RpID, @JobIDwhile @@Fetch_Status = 0beginif (@ID = @JobID)set @cnt = @cnt + 1elseset @cnt = 1update @reprintinfo set Reprintcount = @cntwhere ReprintID = @RpID set @ID = @JobIDfetch next from c1 into @RpID,@JobIDendclose c1deallocate c1SELECT * FROM @reprintinfo
As you can see there is a slight difference in the result, which may or maynot be acceptable.-yan