USE [EnVisage] GO begin tran declare @parentId uniqueidentifier declare @prevparentId uniqueidentifier declare @partNum int declare @Id uniqueidentifier declare @num int set @num = 1 declare dupcursor cursor for select Id, [ParentProjectId] from Project where [ParentProjectId] is not null order by [ParentProjectId], partNum open dupcursor fetch next from dupcursor into @Id, @parentId while @@FETCH_STATUS = 0 begin if @prevparentId is null or @prevparentId <> @parentId begin --print @prevparentId --print @parentId set @num = 1 set @prevparentId = @parentId end update Project set partnum = @num where Id = @Id set @num = @num + 1 --print @num fetch next from dupcursor into @Id, @parentId end close dupcursor; deallocate dupcursor; commit tran