1

Hi I have 2 tables below:

table1

GroupName PersonName email phone
A         Tom        tom@  123
A         Jen        jen@  223
B         Kim        kim@  232

table2

GroupName PersonName email phone
A         Tom        NULL  1
A         Jen        NULL  1
A         Ken        NULL  1
B         Kim        NULL  1
B         Tai        NULL  1

I need to insert all columns of the additional records from table2 into table1. for example, I need Ken and Tai's records from table2 added to table1. it's running on SQL Server 2000 so I cannot use EXCEPT or INTERSECT.

1
  • what do you want to insert? Commented Mar 8, 2013 at 1:18

2 Answers 2

1

assuming email is unique:

insert into table1
select * from table2
where email not in (select email from table1)

alternative:

insert  into table1
select  a.* 
from    table2 a
        LEFT JOIN table1 b
            ON a.email = b.email
WHERE   b.email IS NULL
Sign up to request clarification or add additional context in comments.

4 Comments

use JOIN instead of IN :)
JOINs are way aster than IN
sorry - email is actually NULL in table2 - pls see my edit. thanks
If you want to get technical, use OUTER JOIN instead of IN. But performance is not an absolute. based on the sample data, IN is more than adequate. You could also use NOT EXISTS which might be faster for a larger dataset
0

this will do it..

Inser Table1
select  GroupName, PersonName, email, phone
from Table2 where  GroupName+'|'+PersonName+'|'+email+'|'+phone 
    not in (select GroupName+'|'+PersonName+'|'+email+'|'+phone from Table1)

Thanks!

@leo.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.