SQL Server history table - populate through SP or Trigger?

前端 未结 11 1801
遥遥无期
遥遥无期 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:16

    Recommended approach depends on your requirements. If the history table is there for audit trail, you need to capture each operation. If history table is only for performance reasons, then a scheduled SQL Agent data transfer job should be enough.

    For capturing each operation use either AFTER TRIGGERs or Change Data Capture.

    After triggers provide you with two temp tables to operate with inside the trigger:

    • INSERTED after INSERT or UPDATE
    • DELETED after DELETE

    You can perform inserts to the history table from these temp tables and your history table will always be up-to-date. You might want to add version numbering, time stamps or both in the history table to separate changes to a single source row.

    Change Data Capture (CDC) is designed for creating a delta table that you can use as a source for loading data into a data warehouse (or a history table). Unlike triggers, CDC is asynchronous and you can use any method and scheduling for populating your destination (sprocs, SSIS).

    You can access both original data and changes with CDC. Change Tracking (CT) only detects changed rows. It is possible to construct a complete audit trail with CDC but not with CT. CDC and CT are both only available in the MSSQL 2008 Enterprise and Developer Editions.

提交回复
热议问题