Insert records from two tables

  • I have two tables TableA and TableB

    TableA

    CUST_NUM, ITEM_NUM, SERIAL_NUM

    TableB

    CUST_NUM, ITEM_NUM, SERIAL_NUM

    I need to insert data from TableB into TableA; TableB will contain some records that are already in TableA so I only want to include the records that are not currently in TableA based on ITEM_NUM, SERIAL_NUM. There will be duplicate SERIAL_NUM records in TableB but with different a ITEM_NUM which is fine. Hope I explained this adequately. Any help would be greatly appreciated.

  • Is this homework? And if it is, how much help are you allowed to have from sources like this one?

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • No this is not home work, it is a legitimate request, it may seem a simple problem to you but when one is unsure how to accomplish a particular task it is not so simple.

  • INSERT INTO TableA (CUST_NUM, ITEM_NUM, SERIAL_NUM)

    SELECT CUST_NUM, ITEM_NUM, SERIAL_NUM

    FROM TableB

    WHERE NOT EXISTS (SELECT 1

    FROM TableA

    WHERE TableA.ITEM_NUM = TableB.ITEM_NUM AND

    TableA.SERIAL_NUM = TableB.SERIAL_NUM)

  • Sorry, for the formatting. It seems no matter how I format the code this site add/strips spaces.

  • No problem Adam, thanks for the help I will try it in the AM

  • Thanks Adam it worked like a charm.

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply