cursor to identify changed,deleted and added records

  • need to write a cursor to compare two tables(original and updated tables)

    1.display changes to field values in existing records

    2.new records added

    3.existing records deleted

  • Do the tables have the same unique key that you could join on? If they do you do not need a cursor. You could use a full outer join and case statements. Something like:

    Declare @changes Table (change_type varchar(25), [field list])

    Insert Into @changes

    Select -- get new records

    'New Records',

    O.*

    From

    original_table O Left Join

    update_table U On

    O.unique_key_fields = U.unique_key_fields

    Where

    U.key_fields is null

    UNion

    Select -- get deleted records

    'Deleted Records',

    O.*

    From

    original_table O Right Join

    update_table U On

    O.unique_key_fields = U.unique_key_fields

    Where

    O.key_fields is null

    -- get modified records

    Insert Into @changes

    Select

    'modified records',

    Case

    When O.field1 <> U.field1 then O.field1 + '|' + U.field1 -- concatentating the fields to display different values

    Else 'Not Changed'

    End as field1 -- add as many fields as needed

    From

    original_table O Join

    update_table U On

    O.unique_key_fields = U.unique_key_fields

    Where

    O.field_list_concatenated <> U.field_list_concatenated

    Select * from @changes

    This should perform better than a cursor. Not sure exactly what your requirements are for output.

  • Thank u for ur response.........i will try this out............

  • they asked to use curser becoz it needs to check the records every time for the changes for every baselineobject...........

  • chinni (3/30/2008)


    they asked to use curser becoz it needs to check the records every time for the changes for every baselineobject...........

    Doesn't Jack's script do that?

    If not, then you'll have to explain things a little better. Check this link out for tips on how to get better answers to your questions on the forums: http://www.sqlservercentral.com/articles/Best+Practices/61537/

    [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]

  • chinni (3/30/2008)


    they asked to use curser becoz it needs to check the records every time for the changes for every baselineobject...........

    I don't understand... why do you think that requires a Cursor?

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

  • Maybe because he/she means the broader meaning of cursor.

    Every select creates a cursor.

    Comparing two tables is IMHO a bad solution, because over time there will be a lot of differences, so a compare would take a lot of time. Even if there are not a lot of changes, compare takes some time, if table is huge. If you synchronize the tables, you lose the history. Also, it gets a lot more complicated, when the table has references, which usually does.

    A good solution would be logging changes into a history table with triggers.

    1. triggers decide what and how to log changes

    2. you can have human readable change log

    3. you can selectively purge parts of history that become obsolete.

  • Robert (3/31/2008)


    Maybe because he/she means the broader meaning of cursor.

    Considering the level of the question, I wouldn't think so. I sure wish the OPs would answer these questions so we actually know instead of guessing. 😉 Thinking that a cursor is needed means that, in most cases, something is missing from the problem description.

    Also, a merge of this type doesn't necessarily cause a loss of "history". It depends on how it is written. But I don't know how it needs to be written because you keep trying to answer questions for the OP 😉

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

  • What does "OP" stand for? Ops, original poster :blush:

    Considering the level of the question, I wouldn't think so either. However, when dealing with customers or assisting fellows developers on their learning path, you suspect, but never assume things based on missing information. 😉

  • i have a bunch of tables to be compared.........all the table names are in one virtual table.............

    thanks for ur response

  • if we use left join we display all records in left table.......but we need to display only new record.........

  • chinni (3/31/2008)


    if we use left join we display all records in left table.......but we need to display only new record.........

    That is where the Where U.key_fields is null comes in, U.key_fields represents the data in the Right Table. When it is NULL there is not record, thus it will show only the new records.

  • Robert (3/31/2008)


    What does "OP" stand for? Ops, original poster :blush:

    Considering the level of the question, I wouldn't think so either. However, when dealing with customers or assisting fellows developers on their learning path, you suspect, but never assume things based on missing information. 😉

    Yes... it stands for "Original Poster".

    So far as the other, that's why I didn't want you to answer the question... 😉

    --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 13 posts - 1 through 12 (of 12 total)

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