Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
BitsAndBytes
Starting Member
10 Posts |
Posted - 2014-04-22 : 15:57:57
|
Hi everyone,I have a table called "Names" and I want to use the following sql to insert from a source table into the target table "Names" but I don't know how to code the WHERE part so that I don't insert duplicates. This simple Names table has three columns. NameID, FirstName and LastName. I don't want to insert a record from the source table if the combination of the FirstName and LastName already exist in the target table "Names". Can someone please give me a hand with this?INSERT INTO table(column1, column2, ... )SELECT expression1, expression2, ...FROM source_tablesWHERE conditions; |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2014-04-22 : 16:00:23
|
INSERT INTO table(column1, column2, ... )SELECT expression1, expression2, ...FROM source_tables stWHERE NOT EXISTS (select * from table t where st.column1 = t.column1 and st.column2 = t.column2)Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
BitsAndBytes
Starting Member
10 Posts |
Posted - 2014-04-22 : 16:36:29
|
That worked great. Thank you so much. |
|
|
|
|
|