Log changes to database table with trigger

前端 未结 12 2540
醉梦人生
醉梦人生 2021-02-08 18:48

I am looking for a good way to log changes that occur on a particular set of tables in my SQL Server 2005 database. I believe the best way to do this is through a trigger that g

12条回答
  •  天命终不由人
    2021-02-08 19:24

    There is a pattern for creating such these triggers called Log Trigger. This is vendor independent and very simple. It is described in here.

    The changes are recorded on another history table. There is no way to grab the exact statement, but it is possible to detect if it was an insertion, and update or a deletion because it creates a "chained" set of records. An insertion is a record with no predecessor, a deletion is a record with no successor, intermediate records are updates. Changes can be detected comparing a record against its predecessor.

    It is very easy to get a snapshot of a single entity (or the whole table) at a given point of time.

    As a bonus, the syntax of this pattern for SQL Server happens to be the simplest one, compared to Oracle, DB2 and MySQL.

提交回复
热议问题