forum

Home / DeveloperSection / Forums / How to Insert mutiple unique rows into a table

How to Insert mutiple unique rows into a table

marcel ethan 1734 07-Jan-2015

I am not familiar with SQL that much. I'm trying to insert multiple rows of data into a table that if there exist a row with with duplicate value in BusinessFilterPhrase column then just don't insert. I wrote a pseudocode of what I think it should be.

if (filterCategoryList != null)
{
   foreach (KeyValuePair<string, int> filter in filterCategoryList)
   {
      cmd.CommandText = "insert into tblBusinessName (BusinessFilterPhrase,BusinessCategoryID)" +
                        "select @BusinessFilterPhrase,@BusinessCategoryID" +
                        "from tblBusinessName as t1" +
                        "where NOT EXISTS" +
                        "( select * from tblBusinessName as d1 where d1.BusinessFilterPhrase = @BusinessFilterPhrase) ";
 
      cmd.Parameters.AddWithValue("@BusinessFilterPhrase", filter.Key);
      cmd.Parameters.AddWithValue("@BusinessCategoryID", filter.Value.ToString());
 
      cmd.ExecuteNonQuery();
   }
}

 


Updated on 08-Jan-2015

Can you answer this question?


Answer

1 Answers

Liked By