Some SQL script i used to detect/remove double entries

DECLARE @MinID int;
DECLARE @MaxID int;
DECLARE @ID int;

SELECT @MinID = MIN(ProductID) from Product;
SELECT @MaxID = MAX(ProductID) from Product;
SELECT @ID = ProductID from Product WHERE FK_ProductTypeID=’4′;

SELECT TOP 1 Title
FROM Product
WHERE ProductID < @ID OR @ID=@MinID
ORDER BY ProductID DESC;

SELECT TOP 1 Title
FROM Products
WHERE ProductID > @ID OR @ID=@MaxID
ORDER BY ProductID ASC;

DECLARE @ArticleNumber Varchar(200);
DECLARE @MinProductID Int;
DECLARE @NumDuplicates Int;

DECLARE Product_Cursor CURSOR FOR
SELECT ArticleNumber, min(IDProduct) as MinProductID, count(idproduct) as NumDuplicates
FROM Products
group by ArticleNumber
having count(idproduct) > 1
OPEN Product_Cursor;
FETCH NEXT FROM Product_Cursor INTO @ArticleNumber, @MinProductID, @NumDuplicates;
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM Product_Cursor INTO @ArticleNumber, @MinProductID, @NumDuplicates;
UPDATE LookProducts SET ProductID = @MinProductID WHERE ProductID IN (SELECT IDProduct FROM Products WHERE ArticleNumber = @ArticleNumber);
DELETE FROM Products WHERE IDProduct IN (SELECT IDProduct FROM Products WHERE ArticleNumber = @ArticleNumber AND IDProduct <> @MinProductID);
END;
CLOSE Product_Cursor;
DEALLOCATE Product_Cursor;
GO

comments

No comments yet.

Sorry, the comment form is closed at this time.