SQL Server history table - populate through SP or Trigger?

前端 未结 11 1735
遥遥无期
遥遥无期 2020-11-28 20:43

In my SQL Server backend for my app, I want to create history tables for a bunch of my key tables, which will track a history of changes to the rows.

My entire appli

11条回答
  •  有刺的猬
    2020-11-28 21:36

    Triggers. Here is my approach:

    1. Create one audit table for each critical table that requires audit trial
    2. Audit table will include all columns from source table + columns audit record info such as who, when and the action
    3. Trigger for UPDATE and DELETE only, the INSERT operation will have the pristine record in source table itself
    4. Before update or delete, copy the original record + audit info to audit table
    5. (Optionally - for UPDATE only:) To know which column got updated, use either UPDATE(ColumnName) or COLUMNS_UPDATED() built in SQL function to determine the affected columns

    Auditing this way keeps the current status in the source table and all the history in audit table and easily identified by the key columns.

提交回复
热议问题