Check for changes to an SQL Server table?

前端 未结 8 1161
有刺的猬
有刺的猬 2020-12-04 08:02

How can I monitor an SQL Server database for changes to a table without using triggers or modifying the structure of the database in any way? My preferred programming enviro

8条回答
  •  广开言路
    2020-12-04 08:37

    Unfortunately CHECKSUM does not always work properly to detect changes.

    It is only a primitive checksum and no cyclic redundancy check (CRC) calculation.

    Therefore you can't use it to detect all changes, e. g. symmetrical changes result in the same CHECKSUM!

    E. g. the solution with CHECKSUM_AGG(BINARY_CHECKSUM(*)) will always deliver 0 for all 3 tables with different content:

    
    SELECT CHECKSUM_AGG(BINARY_CHECKSUM(*)) FROM 
    (
      SELECT 1 as numA, 1 as numB
      UNION ALL
      SELECT 1 as numA, 1 as numB
    )  q
    -- delivers 0!

    SELECT CHECKSUM_AGG(BINARY_CHECKSUM(*)) FROM ( SELECT 1 as numA, 2 as numB UNION ALL SELECT 1 as numA, 2 as numB ) q -- delivers 0!

    SELECT CHECKSUM_AGG(BINARY_CHECKSUM(*)) FROM ( SELECT 0 as numA, 0 as numB UNION ALL SELECT 0 as numA, 0 as numB ) q -- delivers 0!

提交回复
热议问题