---
title: "How to Insert mutiple unique rows into a table"  
description: "How to Insert mutiple unique rows into a table"  
author: "marcel ethan"  
published: 2015-01-07  
updated: 2015-01-08  
canonical: https://www.mindstick.com/forum/12850/how-to-insert-mutiple-unique-rows-into-a-table  
category: "asp.net"  
tags: ["c#", "sql server"]  
reading_time: 1 minute  

---

# How to Insert mutiple unique rows into a table

I am not familiar with SQL that much. I'm [trying](https://answers.mindstick.com/qa/93698/6-mistakes-couples-are-trying-to-save-money) to [insert multiple](https://www.mindstick.com/forum/399/how-to-insert-multiple-values-selected-in-checkbox-in-database) [rows of data](https://www.mindstick.com/forum/157166/what-is-the-best-and-fast-way-to-insert-2-million-rows-of-data-into-sql-server) into a [table](https://www.mindstick.com/articles/43918/how-to-design-table-using-bootstrap) that if there exist a row with with [duplicate](https://www.mindstick.com/forum/160911/sql-query-to-find-duplicate-records-in-a-table-in-sql-server) [value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages) in BusinessFilterPhrase [column](https://www.mindstick.com/forum/33860/how-to-calculate-column-summary-in-sql-server) 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();   }}
```

## Replies

### Reply by Takeshi Okada

You code looks correct. I would write it as:

```
insert into tblBusinessName(BusinessFilterPhrase, BusinessCategoryID)    select @BusinessFilterPhrase, @BusinessCategoryID    from tblBusinessName t1    where NOT EXISTS (select 1                      from tblBusinessName d1                      where d1.BusinessFilterPhrase = @BusinessFilterPhrase                     )
```

(The changes are only cosmetic.)

EDIT:

If performance is an issue, create an index on BusinessFilterPhrase:

create index idx_tblBusinessName_BusinessFilterPhrase on tblBusinessName(BusinessFilterPhrase);

You can make this a unique index, if you want the database to enforce the uniqueness of the column (it will generate an error when duplicate values would be inserted).


---

Original Source: https://www.mindstick.com/forum/12850/how-to-insert-mutiple-unique-rows-into-a-table

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
