Merging Tables

  • Hey All,

    I am merging two tables from separate databases that may contain duplicate entries in two columns, account and account_num. If there is a duplication, I want to add 'OLD' at the end of each entry in the originating table. The destination entry should stay the same. I'm a bit of a TSQL newbie and I could use some help getting a step in the right direction.

    Thanks in advance!

  • Maybe you need something like this.

    Update Main set Col = 'old' from dbo.OldTable Main inner join

    (

    Select O.Oldid from OldDb.dbo.OldTable O inner join

    NewDB.dbo.NewTable N on O. Oldid = N.NewId) dtDuplicates

    on Main.Oldid = dtDuplicates.Oldid

  • Check the results before you insert

    INSERT INTO Database1.dbo.Account (Cols) Values

    SELECT * FROM

    (

    SELECT COALESCE(DB1.Account, DB2.Account + 'OLD') Account,

     DB2.account_num,

     DB2.RestOftheColumns

    FROM

     Database2.dbo.Account DB2

    LEFT OUTER JOIN

     Database1.dbo.Account DB1

    ON

     DB2.account = DB21.account AND

     DB2.account_num = DB21.account_num) Results

    Regards,
    gova

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

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