How best to log changes to history

  • I have a table for which we want to log changes to various columns in a history table.  I can do an if exists query on each column, or I can set variables to each column in both inserted and deleted and compare the variables.  One performs a large number of select statements, while the other is just large and ugly looking.

    Is there a way I'm not considering that is better than these 2 methods, and which method is the most efficient?

  • You could use checksum function to compare both data.

    If the numbers returned are different, then changes where made.

     

  • or

    create trigger mytrig on mytable for update

    as

    insert myhistory( myval1, myval1)

     select i.myval1, i.myval2

     from inserted i inner join deleted d on i.pk = d.pk

     where i.myval1 <> d.myval1

     

    repeat.

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

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