How to merge two tables (update/insert)?

  • I want to merge data from one table to another(if unique key found then update, else insert). These tables reside on different databases on the same SQL Server. Can some suggest the best way of doing this. Using a stored procedure or DTS or any other idea.

  • I do this sort of thing frequently and use SP to do it. First I update first table by inner join second table on key. Then I left outer join the tables again and insert missing rows (key is null). I do it in this sequence so that I do not have the overhead of updating inserted rows.

    Edited by - davidburrows on 11/13/2003 06:05:56 AM

    Far away is close at hand in the images of elsewhere.
    Anon.

  • Very similar to David's suggestion:

    1. Put the data in a temporary table. 2. Join the tables and update the matching records in the destination table. 3. Join the tables again, this time deleting the matching records from the temporary table. 4. Insert any records left in the temp table to the destination table.

    JM

  • Thanks guys!!

Viewing 4 posts - 1 through 3 (of 3 total)

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