Home Forums SQL Server 2008 T-SQL (SS2K8) Compare the data in two tables, If Different, Update RE: Compare the data in two tables, If Different, Update

  • Kingston Dhasian (2/26/2013)


    There are 3 rows which are different as per the sample data provided

    UPDATEOD

    SETOD.FirstName = UD.FirstName,

    OD.LastName = UD.LastName,

    OD.Birthdate = UD.Birthdate,

    OD.AreaCode = UD.AreaCode,

    OD.PhoneNumber = UD.PhoneNumber

    FROM#OriginalData AS OD

    INNER JOIN #UpdatedData AS UD

    ONOD.MemberID = UD.MemberID AND OD.LocationID = UD.LocationID

    WHEREOD.FirstName != UD.FirstName

    OROD.LastName != UD.LastName

    OROD.Birthdate != UD.Birthdate

    OROD.AreaCode != UD.AreaCode

    OROD.PhoneNumber != UD.PhoneNumber

    Oh, be careful. You could have quite the surprise if NULLs are involved. I'm a bit stuck in the world of 2005 so haven't had to work with MERGE but I believe the same problem with NULLs may exist there, as well.

    You might want to try EXCEPT which does observe NULLs.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)