SQL Change Tracking in SQL Server 2008
What is Change Tracking?
Change tracking is a new feature in SQL Server 2008 that allows applications to query for information about the changes that have been made to user tables since a previous point in time. Change tracking is intended to be used as a building-block used by synchronization components or applications.
In order to enable change tracking at the table level, one needs to enable it at the table level and then needs to be enabled at the database level. The default value change tracking is not enabled for a database
Enabling Change Tracking at the database level
1. Open the SQL Server Management Studio and connect to SQL Server 2008
2. Open Object Explorer and expand Databases.
3. Select the particular database where the change tracking needs to be enabled. Right click the database and select properties to launch the properties dialog.
4. Navigate to “Change Tracking” page and change the value of change tracking from false to true.
5. Enter the required values for Retention Period, Retention Period Units and Auto Cleanup.
- Retention Period: Change information will be retained for atleast the time period that is specified by the retention period and retention period units.
- Retention Period Units: The units of the Retention Period Days or Months or Years.
- Auto Cleanup: Automatically clean up the change tracking information by using the specified retention period. If this value is false, change tracking information automatically continues to grow.
Click ‘Ok ‘ to execute the action or Click ‘Script’ to generate the T-SQL for this action.
Enabling Change Tracking at the table level:
1. Open the SQL Server Management Studio and connect to SQL Server 2008
2. Open Object Explorer and expand Databases and select the database and expand the tables under it.
3. Select the particular table where the change tracking needs to be enabled. Right click the table and select properties to launch the properties dialog.
4. Navigate to “Change Tracking” page and change the value of change tracking from false to true.
5. Enter the required value for the track columns updated field. This field is used to indicate the columns which are changed by UPDATE operation and also indicates that row has changed.
Click ‘Ok ‘ to execute the action or Click ‘Script’ to generate the T-SQL for this action.
Note: Change tracking is supported only on the tables having primary key.
In case if one wants to view the records which are changed after enabling the change tracking, there is no corresponding for it. But using T-SQL, one can find it easily.
In order to find the rows changed on a table, we needs to use CHANGETABLE(CHANGES …)and CHANGE_TRACKING_CURRENT_VERSION().
T-SQL for viewing the records changed
-- Assuming dbo.t is the table to view the records changed in it
DECLARE @sync_version int;
DECLARE @last_sync_version int;
SET @sync_version = CHANGE_TRACKING_CURRENT_VERSION();
-- Obtain incremental changes using the sync version obtained last time
SELECT
P.<columnname>,
CT.SYS_CHANGE_OPERATION, CT.SYS_CHANGE_COLUMNS, CT.SYS_CHANGE_CONTEXT
FROM
<tablename> AS P
RIGHT OUTER JOIN
CHANGETABLE(CHANGES dbo.d, 0) AS CT
ON
P.c1 = CT.c1
CHANGETABLE() returns the table containg the below columns :
SYS_CHANGE_OPERATION – Type of DML operation on a row.
SYS_CHANGE_COLUMNS – Columns changed from a baseline version
SYS_CHANGE_CONTEXT – Optionally specified with the WITH clause
SYS_CHANGE_VERSION – Current change version associated with a row.
Comments
Anonymous
April 21, 2009
The comment has been removedAnonymous
April 18, 2011
Hello Timithy The script is meant for viewing changed records in dbo.t table. Thanks Sreekar MAnonymous
February 04, 2013
Very Nice Article. You can find some more additional information from my Article www.codeproject.com/.../SQL-Server-Change-Tracking-CTAnonymous
June 25, 2014
Fantastic approach ! A well described blog that cover all SQL server audit aspects in sort, you can explore and read in depth at here : sqlserverauditing.blogspot.in/.../track-all-critical-and-granular-changes.htmlAnonymous
December 29, 2014
Great article, you can also check this 2 parts article on SQL Server Change tracking , Part 1 - sqlturbo.com/practical-intro-sql-server-table-change-tracking and Part 2 sqlturbo.com/practical-intro-sql-server-table-change-tracking-part-2-column-trackingAnonymous
January 04, 2015
Excellent, thanks for sharing most valuable information regarding to how track changes in sql server and i found really good information from www.lepide.com/sql-server-audit to monitor sql server changes and auditing access permission and track all SQL servers in the network to audit each and every changes from a centralized platform . This tool instantly generates real time alert to sensitive changes and export the report in different files format.