1 DECLARE @id UNIQUEIDENTIFIER 2 3 DECLARE myCursor CURSOR 4 FOR 5 SELECT ProductId 6 FROM ( SELECT ProductId , 7 COUNT(1) cou 8 FROM dbo.T_ProductDescription 9 GROUP BY ProductId 10 HAVING COUNT(1) > 1 11 ) tmp 12 13 OPEN myCursor 14 15 FETCH NEXT FROM myCursor INTO @id 16 17 WHILE @@FETCH_STATUS = 0 18 BEGIN 19 20 UPDATE l 21 SET l.Sort = num 22 FROM dbo.T_ProductDescription l 23 INNER JOIN ( SELECT l.Id , 24 ROW_NUMBER() OVER ( ORDER BY l.Sort ASC ) num 25 FROM dbo.T_ProductDescription l 26 WHERE l.ProductId = @id 27 ) tmp ON l.Id = tmp.Id 28 29 30 FETCH NEXT FROM myCursor INTO @id 31 END 32 33 CLOSE myCursor 34 DEALLOCATE myCursor