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 |
Round
Starting Member
6 Posts |
Posted - 2007-01-24 : 08:06:17
|
Hello all,I have two customer tables. I want to insert all the customers and their details from table 1 into table 2, but only if they are not already in table 2. Is this possible?Regards |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-01-24 : 08:08:37
|
Yes it is possible.Insert into Tbl2(col1, col2, ....)Select col1, col2, ....From Tbl1 t1Left Join Tbl2 t2on t1.pk = t2.pkwhere t2.pk is NULL Note: pk = primary key columnHarsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
Round
Starting Member
6 Posts |
Posted - 2007-01-24 : 08:57:05
|
Table 1 does not have the same primary key as Table 2.Table 1's columns:- customer_id(pk), forename, surname.Table 2's columns:- ID(pk), customer_id, forename, surname.Many Thanks |
 |
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2007-01-24 : 10:25:21
|
What do u want in each Column of Table 2 to be having once the query is runMainly refered to : ID(pk) & customer_idV can see that Table1.forename --> Table2.forename & Table1.surname --> Table2.surnameSrinika |
 |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-01-24 : 10:30:15
|
[code]Insert into Tbl2(customer_id, forename, surname)Select customer_id, forename, surnameFrom Tbl1 t1Left Join Tbl2 t2on t1.customer_id = t2.customer_idwhere t2.customer_id is NULL[/code]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
|
|
|
|