views:

67

answers:

3

I have 2 table TableA and TableB I want to insert all records at a time from TableA to TableB if the records are not in TableB

Please help thank you

+2  A: 

Assuming they are sharing the same primary key.

insert TableB
select A.* 
from TableA A 
left join TableB B ON A.pk = B.pk 
where B.pk is null
Sam Saffron
+2  A: 

This should work

INSERT INTO TableB
SELECT * FROM TableA
EXCEPT
SELECT * FROM TableB
Coentje
A: 

Alternate form of sambo's answer.

INSERT TableB
SELECT *
FROM TableA A
WHERE NOT EXISTS (
    SELECT *
    FROM TableB B
    WHERE A.pk = B.pk )
MikeW
Whoever down marked this is not acting with reason. This works perfectly fine... I do prefer the LEFT JOIN approach when comparing tables, but this is NOT wrong...
Dems
+1 to spite the down marker...
Dems
Why thank you Dems. In fact it might perform significantly different (better or worse) but it's a very common pattern.
MikeW