<p>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 <a href="http://en.wikipedia.org/wiki/.NET_Framework" rel="noreferrer">.NET</a> and C#.</p>

<p>I'd like to be able to support any <a href="http://en.wikipedia.org/wiki/Microsoft_SQL_Server#Genesis" rel="noreferrer">SQL Server 2000</a> 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.</p>

<p>By <em>"changes to a table"</em> I mean changes to table data, not changes to table structure.</p>

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

<hr>

<p>The best course of action given my requirements (no triggers or schema modification, SQL Server 2000 and 2005) seems to be to use the <code>BINARY_CHECKSUM</code> function in <a href="http://en.wikipedia.org/wiki/Transact-SQL" rel="noreferrer">T-SQL</a>. The way I plan to implement is this:</p>

<p>Every X seconds run the following query:</p>

<pre><code>SELECT CHECKSUM_AGG(BINARY_CHECKSUM(*))
FROM sample_table
WITH (NOLOCK);
</code></pre>

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

<pre><code>SELECT row_id, BINARY_CHECKSUM(*)
FROM sample_table
WITH (NOLOCK);
</code></pre>

<p>And compare the returned checksums against stored values.</p>

[{'answer_id': 51, 'author': 'Nick Berardi', 'author_id': 17, 'author_profile': 'https://Stackoverflow.com/users/17', 'pm_score': 5, 'selected': False, 'text': "<p>Why don't you want to use triggers?  They are a good thing if you use them correctly.  If you use them as a way to enforce referential integrity that is when they go from good to bad.  But if you use them for monitoring, they are not really considered taboo.</p>\n"}, {'answer_id': 82, 'author': 'Yaakov Ellis', 'author_id': 51, 'author_profile': 'https://Stackoverflow.com/users/51', 'pm_score': 4, 'selected': False, 'text': '<p>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 <a href="http://msdn.microsoft.com/en-us/library/ms186778.aspx" rel="noreferrer">INFORMATION_SCHEMA</a> 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.</p>\n\n<p>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):</p>\n\n<pre><code>select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = \'ABC\'\n</code></pre>\n\n<p>You would monitor different columns and INFORMATION_SCHEMA views depending on how exactly you define "changes to a table".</p>\n'}, {'answer_id': 352, 'author': 'Jon Galloway', 'author_id': 5, 'author_profile': 'https://Stackoverflow.com/users/5', 'pm_score': 8, 'selected': True, 'text': '<p>Take a look at the CHECKSUM command:</p>\n\n<pre><code>SELECT CHECKSUM_AGG(BINARY_CHECKSUM(*)) FROM sample_table WITH (NOLOCK);\n</code></pre>\n\n<p>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:</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/aa258245(SQL.80).aspx" rel="noreferrer">CHECKSUM</a></p>\n\n<p>Here\'s how I used it to rebuild cache dependencies when tables changed:<br>\n<a href="http://weblogs.asp.net/jgalloway/archive/2005/05/07/406056.aspx" rel="noreferrer">ASP.NET 1.1 database cache dependency (without triggers)</a></p>\n'}, {'answer_id': 695, 'author': 'Chris Miller', 'author_id': 206, 'author_profile': 'https://Stackoverflow.com/users/206', 'pm_score': 4, 'selected': False, 'text': "<p>How often do you need to check for changes and how large (in terms of row size) are the tables in the database?  If you use the <code>CHECKSUM_AGG(BINARY_CHECKSUM(*))</code> method suggested by John, it will scan every row of the specified table.  The <code>NOLOCK</code> hint helps, but on a large database, you are still hitting every row.  You will also need to store the checksum for every row so that you tell one has changed.</p>\n\n<p>Have you considered going at this from a different angle?  If you do not want to modify the schema to add triggers, (which makes a sense, it's not your database), have you considered working with the application vendor that does make the database?  </p>\n\n<p>They could implement an API that provides a mechanism for notifying accessory apps that data has changed.  It could be as simple as writing to a notification table that lists what table and which row were modified.  That could be implemented through triggers or application code.  From your side, ti wouldn't matter, your only concern would be scanning the notification table on a periodic basis.  The performance hit on the database would be far less than scanning every row for changes.</p>\n\n<p>The hard part would be convincing the application vendor to implement this feature.  Since this can be handles entirely through SQL via triggers, you could do the bulk of the work for them by writing and testing the triggers and then bringing the code to the application vendor.  By having the vendor support the triggers, it prevent the situation where your adding a trigger inadvertently replaces a trigger supplied by the vendor.</p>\n"}, {'answer_id': 1778, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 4, 'selected': False, 'text': "<p>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?</p>\n"}, {'answer_id': 3009, 'author': 'caryden', 'author_id': 313, 'author_profile': 'https://Stackoverflow.com/users/313', 'pm_score': 4, 'selected': False, 'text': '<p>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 <code>SQLDependency</code> class in <code>System.Data.SqlClient</code>. See <a href="http://msdn.microsoft.com/en-us/library/t9x04ed2.aspx" rel="noreferrer">Query Notifications in SQL Server (ADO.NET)</a>.</p>\n'}, {'answer_id': 5486054, 'author': 'BitLauncher', 'author_id': 519971, 'author_profile': 'https://Stackoverflow.com/users/519971', 'pm_score': 5, 'selected': False, 'text': "<p><b>Unfortunately CHECKSUM does not always work properly to detect changes</b>.</p>\n\n<p>It is only a primitive checksum and no cyclic redundancy check (CRC) calculation.</p>\n\n<p>Therefore you can't use it to detect all changes, e. g. symmetrical changes result in the same CHECKSUM!</p>\n\n<p>E. g. the solution with <code>CHECKSUM_AGG(BINARY_CHECKSUM(*))</code> will always deliver 0 for all 3 tables with different content:</p>\n\n<p><pre><code>\nSELECT CHECKSUM_AGG(BINARY_CHECKSUM(*)) FROM \n(\n  SELECT 1 as numA, 1 as numB\n  UNION ALL\n  SELECT 1 as numA, 1 as numB\n)  q\n-- delivers 0!</p>\n\n<p>SELECT CHECKSUM_AGG(BINARY_CHECKSUM(*)) FROM \n(\n  SELECT 1 as numA, 2 as numB\n  UNION ALL\n  SELECT 1 as numA, 2 as numB\n)  q\n-- delivers 0!</p>\n\n<p>SELECT CHECKSUM_AGG(BINARY_CHECKSUM(*)) FROM \n(\n  SELECT 0 as numA, 0 as numB\n  UNION ALL\n  SELECT 0 as numA, 0 as numB\n)  q\n-- delivers 0!\n</pre></code></p>\n"}, {'answer_id': 24925434, 'author': 'ECE', 'author_id': 1877309, 'author_profile': 'https://Stackoverflow.com/users/1877309', 'pm_score': 3, 'selected': False, 'text': '<p>Check the last commit date. Every database has a history of when each commit is made. I believe its a standard of ACID compliance.</p>\n'}]
['https://Stackoverflow.com/questions/36', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/32/']