Skip to content

Instantly share code, notes, and snippets.

@clintmjohnson
Created June 14, 2015 13:57
Show Gist options
  • Save clintmjohnson/44f63ef816903d3b2751 to your computer and use it in GitHub Desktop.
Save clintmjohnson/44f63ef816903d3b2751 to your computer and use it in GitHub Desktop.
Delete Duplicate Key Id Values on a SQL Server Table One by One
USE TableName
MERGE TOP (1) Product_Master_Table AS a
USING
(SELECT model_number,COUNT(*) AS dupeCount
FROM Product_Staging_Table
WHERE model_number <> '' AND model_number IS NOT NULL
GROUP BY model_number
HAVING COUNT(*) > 1) AS b
ON a.model_number = b.model_number
WHEN MATCHED
THEN DELETE;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment