Never been to DZone Snippets before?

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

About this user

CornerBLUE, Inc. http://www.cornerblue.com

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

SQL SERVER: Delete Duplicate Rows with Primary Id

Deletes duplicates (leaving one instance) where the table has a primary key. Good for tables with Id, DupColumn, DupColumn...

(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)

SQL: Check For Duplicate Rows

   1  
   2  SELECT email,
   3  COUNT(email) AS NumOccurrences
   4  FROM users
   5  GROUP BY email
   6  HAVING ( COUNT(email) > 1 )

C#: Execute A Query & Return A Reader

   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  }

SQL: Copy Data From One Table Into Another

// SQL Query to copy data from one table and insert it into another

   1  
   2  INSERT INTO TABLE2 (COL1, COL2, COL3) SELECT COL1, COL4, COL7 FROM TABLE1
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS