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 |
|
dcarva
Posting Yak Master
140 Posts |
Posted - 2011-08-10 : 22:24:28
|
| Hello,I need to perform an INSERT INTO and make sure that it does not insert duplicates that may already be in the row. Ex:insert into table1 (field1, field2, field3) select uc.field1, uc.field2, uc.field3 from table2Some records in table2 already exist in table1. In above inserts, I don't want it to insert duplicate records into table1.Thanks! |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-08-10 : 22:27:17
|
you can check for existence of record using NOT EXISTSinsert into table1 . . .from table2 t2where not exists (select * from table1 x where x.pk = t2.pk) KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
dcarva
Posting Yak Master
140 Posts |
Posted - 2011-08-10 : 23:14:15
|
| Thank you!That helped. I initially tried something similar and I could not get it to work. I ended up:insert into table1 (field1, field2, field3)select uc.field1, uc.field2, uc.field3from table2where NOT exists(select field1, field2, field3 from table1 t1 where t1.field1=t2.field1 and t1.field2 = t2.field2 and t1.field3=t2.field3) |
 |
|
|
|
|
|
|
|