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 |
|
maevr
Posting Yak Master
169 Posts |
Posted - 2010-03-08 : 08:13:43
|
| I have three tables that I want to join into a new table, the problem is that the same primary key data existst in the different tables (table1, table2 and table3).I only want to have one and only one row to insert into targetTable.Primary key id1 and id2 is unique together.table1id1, pkid2, pkcol varchar(10)table2id1, pkid2, pkcol varchar(10)table3id1, pkid2, pkcol varchar(10)Target table:targetTableid1, pkid2, pkcol varchar(10)exampletable1id1 id2 col1 1 test (exists in all tables)1 2 testtable2id1 id2 col1 1 test (exists in all tables)1 3 testtable3id1 id2 col1 1 test (exists in all tables)1 4 testOutput:id1 id2 col1 1 test1 2 test1 3 test1 4 testI only want the row in table1 to be inserted into the targetTable |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-03-08 : 08:41:03
|
| [code]INSERT INTO target (id1, id2, col)SELECT id1, id2, col FROM table1UNIONSELECT id1, id2, col FROM table2UNIONSELECT id1, id2, col FROM table3[/code] |
 |
|
|
|
|
|
|
|