Implement data change tracking
In this tutorial we will learn how to enable and disable change tracking for a database and a table.
Enable Change Tracking for a Database
Before you can use change tracking, you must enable change tracking at the database level. The following example shows how to enable change tracking by using ALTER DATABASE.
SQL
ALTER DATABASE AdventureWorks2012
SET CHANGE_TRACKING = ON
(CHANGE_RETENTION = 2 DAYS, AUTO_CLEANUP = ON)
However, you can also enable change tracking in SQL Server Management Studio by using the Database Properties (ChangeTracking Page) dialog box. And, if a database contains memory optimized tables, you can’t enable change tracking with SQL Server Management Studio. To enable, use T-SQL. Further, you can specify the CHANGE_RETENTION and AUTO_CLEANUP options when you enable change tracking. And, you can change the values at any time after change tracking is enabled.
Next, you can use the AUTO_CLEANUP option to enable or disable the cleanup task that removes old change tracking information. This can be useful when there is a temporary problem that prevents applications from synchronizing and the process for removing change tracking information older than the retention period must be paused until the problem is resolved.
For any database that uses change tracking, be aware of the following:
- Firstly, to use change tracking, the database compatibility level must be set to 90 or greater. If a database has a compatibility level of less than 90, you can configure change tracking. However, the CHANGETABLE function, which is used to obtain change tracking information, will return an error.
- Secondly, using snapshot isolation is the easiest way for you to help ensure that all change tracking information is consistent. For this reason, we strongly recommend that snapshot isolation be set to ON for the database.
Enable Change Tracking for a Table
Change tracking must be enabled for each table that you want tracked. When change tracking is enabled, change tracking information is maintained for all rows in the table that are affected by a DML operation.
The following example shows how to enable change tracking for a table by using ALTER TABLE.
SQL
ALTER TABLE Person.Contact
ENABLE CHANGE_TRACKING
WITH (TRACK_COLUMNS_UPDATED = ON)
However, you can also enable change tracking for a table in SQL Server Management Studio by using the Database Properties (ChangeTracking Page) dialog box. And, when the TRACK_COLUMNS_UPDATED option is set to ON, the SQL Server Database Engine stores extra information about which columns were updated to the internal change tracking table. Further, column tracking can enable an application to synchronize only those columns that were updated. This can improve efficiency and performance. However, because maintaining column tracking information adds some extra storage overhead, this option is set to OFF by default.
Disable Change Tracking for a Database or Table
Change tracking must first be disabled for all change-tracked tables before change tracking can be set to OFF for the database. However, to determine the tables that have change tracking enabled for a database, use the sys.change_tracking_tables catalog view. And, when no tables in a database track changes, you can disable change tracking for the database. The following example shows how to disable change tracking for a database by using ALTER DATABASE.
SQL
ALTER DATABASE AdventureWorks2012
SET CHANGE_TRACKING = OFF
The following example shows how to disable change tracking for a table by using ALTER TABLE.
SQL
ALTER TABLE Person.Contact
DISABLE CHANGE_TRACKING;
Reference: Microsoft Documentation