Compare 2 tables

  • Hi there!!

    I have a procedure that compares 2 tables.

    insert into TESTE_CasesModificados

    select * from BizTalkCaseIntermDetail

    where state = 'To Process'

    intersect

    select * from ActualBizTalk

    This statement gives me the info which was altered. But now, I want to know which field was altered. Is there any way to do this or is it impossible?

    Thanks.

  • You can always build a set of left outer joins between the two tables. That'll give you which exact column was modified.

    Something like:

    select t1.Col1

    from dbo.Table1 t1

    left outer join dbo.Table2 t2

    on t1.Col1 = t2.Col1

    where t2.Col1 is null

    That will give you all the places where there is no match between Col1 in Table1 and Table2. If you have a mutual ID number or other unmodified primary key, you can also include those in the join criteria.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • There is no way to do this across all fields without explicitly testing them. It's a pain to build, but if it's needed, you write it once and it's used over and over.

  • GSquared (6/26/2008)


    You can always build a set of left outer joins between the two tables. That'll give you which exact column was modified.

    Something like:

    select t1.Col1

    from dbo.Table1 t1

    left outer join dbo.Table2 t2

    on t1.Col1 = t2.Col1

    where t2.Col1 is null

    That will give you all the places where there is no match between Col1 in Table1 and Table2. If you have a mutual ID number or other unmodified primary key, you can also include those in the join criteria.

    Full outer join whould show both tables...

    select t1.Col1 AS t1Col1,

    t2.Col2 AS t2Col2

    from dbo.Table1 t1

    FULL outer join dbo.Table2 t2

    on t1.Col1 = t2.Col1

    where (t1.Col1 IS NULL

    OR t2.Col1 is null)

    Of course, you could write a little dynamic SQL to generate that code...

    --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)

  • thanks for the replies.

    I will try it. 🙂

  • Or you could use:

    SQLToolbelt from Redgate

    oes allow you to make : database schema differences between 2 databases (or even backups)

    and permits you to see the real datadifferences in 2 databases

    you could use a free trial or buy a license, is not so expensive if you see what you get in return.

    wkr,

    eddy

  • eddy (6/27/2008)


    is not so expensive if you see what you get in return

    I've gotta agee there... what you get is proven code with no bugs... Red Gate is real good about that and that code has been around for a long time...

    --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)

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

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