who - Check for changes to an SQL Server table?

track data changes in sql server 2012 (6)

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 environment is .NET and C#.

I'd like to be able to support any SQL Server 2000 SP4 or newer. My application is a bolt-on data visualization for another company's product. Our customer base is in the thousands, so I don't want to have to put in requirements that we modify the third-party vendor's table at every installation.

By "changes to a table" I mean changes to table data, not changes to table structure.

Ultimately, I would like the change to trigger an event in my application, instead of having to check for changes at an interval.

The best course of action given my requirements (no triggers or schema modification, SQL Server 2000 and 2005) seems to be to use the BINARY_CHECKSUM function in T-SQL . The way I plan to implement is this:

Every X seconds run the following query:

FROM sample_table

And compare that against the stored value. If the value has changed, go through the table row by row using the query:

FROM sample_table

And compare the returned checksums against stored values.

Check the last commit date. Every database has a history of when each commit is made. I believe its a standard of ACID compliance.

Have a DTS job (or a job that is started by a windows service) that runs at a given interval. Each time it is run, it gets information about the given table by using the system INFORMATION_SCHEMA tables, and records this data in the data repository. Compare the data returned regarding the structure of the table with the data returned the previous time. If it is different, then you know that the structure has changed.

Example query to return information regarding all of the columns in table ABC (ideally listing out just the columns from the INFORMATION_SCHEMA table that you want, instead of using *select ** like I do here):


You would monitor different columns and INFORMATION_SCHEMA views depending on how exactly you define "changes to a table".

Take a look at the CHECKSUM command:


That will return the same number each time it's run as long as the table contents haven't changed. See my post on this for more information:


Here's how I used it to rebuild cache dependencies when tables changed:
ASP.NET 1.1 database cache dependency (without triggers)

Unfortunately, I do not think that there is a clean way to do this in SQL2000. If you narrow your requirements to SQL Server 2005 (and later), then you are in business. You can use the SQLDependency class in System.Data.SqlClient . See Query Notifications in SQL Server (ADO.NET) .

Wild guess here: If you don't want to modify the third party's tables, Can you create a view and then put a trigger on that view?

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 1 as numA, 1 as numB
  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!