Home All Groups Group Topic Archive Search About

Insert / Compare on multiple columns

Author
3 Nov 2005 7:22 PM
Jay
TableA contains customer_number,product_id,purchase_date.
TableB contains customer_number,product_id,purchase_date.

I need to insert everything into TableB from TableA where the entire row
from TableA (customer_number,product_id,purhase_date) doesn't already exist
in TableB (customer_number,product_id,purchase_date).  How is this possible?

Thank you  much.

Author
3 Nov 2005 7:28 PM
Anith Sen
Try:

INSERT tblA ( customer_number,product_id,purchase_date )
SELECT customer_number,product_id,purchase_date
  FROM tblB
WHERE NOT EXISTS ( SELECT *
                      FROM tblA
                     WHERE tblA.customer_number = tblB.customer_number
                       AND tblA.product_id = tblB.product_id
                       AND tblA.purchase_date = tblB.purchase_date ) ;

--
Anith
Author
3 Nov 2005 7:37 PM
Trey Walpole
use NOT EXISTS

insert into TableB (customer_number, product_id, purchase_date)
select customer_number, product_id, purchase_date
from TableA a
where not exists (select * from TableB where
customer_number=a.customer_number and product_id=a.product_id and
purchase_date=a.purchase_date)

Jay wrote:

Show quote
>TableA contains customer_number,product_id,purchase_date.
>TableB contains customer_number,product_id,purchase_date.
>
>I need to insert everything into TableB from TableA where the entire row
>from TableA (customer_number,product_id,purhase_date) doesn't already exist
>in TableB (customer_number,product_id,purchase_date).  How is this possible?
>
>Thank you  much.
>
>
>
>
>

>

AddThis Social Bookmark Button