Курсоров следует избегать в максимально возможной степени. Это возвращает один набор результатов. Надеюсь, что это нормально для ваших нужд.
IF OBJECT_ID('tempdb..#MyTempTable') IS NOT NULL
DROP TABLE #MyTempTable;
CREATE TABLE #MyTempTable
(
ItemName NVARCHAR(100) ,
ItemCode NVARCHAR(100) ,
StyleNo NVARCHAR(100) ,
UPC NVARCHAR(100) ,
EAN NVARCHAR(100) ,
UnitFactor NVARCHAR(100) ,
ItemNameGroup NVARCHAR(100)
);
WITH cte
AS ( SELECT [ItemName] ,
[ItemCode] ,
[StyleNo] ,
[UPC] ,
[EAN] ,
[UnitFactor] ,
ROW_NUMBER() OVER ( PARTITION BY [ItemName] ORDER BY [ItemName] ) AS rn
FROM [dbo].[NewTest]
)
INSERT INTO [#MyTempTable]
( [ItemName] ,
[ItemCode] ,
[StyleNo] ,
[UPC] ,
[EAN] ,
[UnitFactor] ,
[ItemNameGroup]
)
SELECT c.[ItemName] ,
c.[ItemCode] ,
c.[StyleNo] ,
c.[UPC] ,
c.[EAN] ,
c.[UnitFactor] ,
t.[ItemName]
FROM [cte] AS c
INNER JOIN ( SELECT [cte].[ItemName]
FROM [cte]
WHERE [cte].[rn] = 1
) t ON c.[ItemName] = t.[ItemName];
SELECT [mtt].[ItemName] ,
[mtt].[ItemCode] ,
[mtt].[StyleNo] ,
[mtt].[UPC] ,
[mtt].[EAN] ,
[mtt].[UnitFactor] ,
[mtt].[ItemNameGroup]
FROM [#MyTempTable] AS [mtt];