SQL SERVER: Delete Duplicate Rows with Primary Id
(This is MS-SQL specific)
1 2 DELETE 3 FROM TableName 4 WHERE Id NOT IN 5 (SELECT MAX(Id) 6 FROM TableName 7 GROUP BY DuplicateColumName1, DuplicateColumName2)
DZone Snippets > cornerblue > sql
13463 users tagging and storing useful source code snippets
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
CornerBLUE, Inc. http://www.cornerblue.com
1 2 DELETE 3 FROM TableName 4 WHERE Id NOT IN 5 (SELECT MAX(Id) 6 FROM TableName 7 GROUP BY DuplicateColumName1, DuplicateColumName2)
1 2 SELECT email, 3 COUNT(email) AS NumOccurrences 4 FROM users 5 GROUP BY email 6 HAVING ( COUNT(email) > 1 )
1 2 public static SqlDataReader GetReader(string Query) 3 { 4 string ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["CONNECTION_STRING_NAME"].ConnectionString; 5 SqlConnection con = new SqlConnection(ConnectionString); 6 SqlCommand command = new SqlCommand(); 7 8 command.Connection = con; 9 command.Connection.Open(); 10 command.CommandText = Query; 11 return command.ExecuteReader(); 12 }
1 2 INSERT INTO TABLE2 (COL1, COL2, COL3) SELECT COL1, COL4, COL7 FROM TABLE1