Enable SQL Server Change Tracking using T-SQL
You can enable the Change Tracking feature for a table to keep track of the rows which are inserted, updated or deleted.
Change tracking is a way for SQL Server automatically keep track of the rows that have been inserted, updated or deleted in a table.
You have to enable the Change tracking at the database level and also on each table that you want to track.
Suppose you want to enable change tracking for your database named APPDB, you can use following tsql script:
ALTER DATABASE APPDB SET CHANGE_TRACKING = ON (CHANGE_RETENTION = 7 DAYS, AUTO_CLEANUP = ON)
GO
Following query list out the database which have Change tracking enabled:
SELECT DB_NAME(database_id) [DATABASE_NAME],* FROM sys.change_tracking_databases
To enable Change tracking for a particular table use the following script, which enable CT for table named EMP
ALTER TABLE dbo.EMP ENABLE CHANGE_TRACKING WITH (TRACK_COLUMNS_UPDATED = OFF)
GO
Use the below script to list out tables which have Change tracking enabled:
SELECT OBJECT_NAME(object_id) [TABLE_NAME],* FROM sys.change_tracking_tables
You can use Change tracking concept to set up a DW project.
Suppose you have the Application DB and its corresponding DW DB. For each table in APP db, there will be a corresponding table in your DW DB in order to keep the latest and history data. In your application database you can enable the change tracking for the tables which you want to keep history in DW. Then implement a mechanism like SQL jobs which check data changes in the tables using SSIS package in your APP DB and push the changes to corresponding DW table . Once the job has run ,latest data which has added to the APP table will get inserted to DW table as the latest row and existing rows will get updated as history records in that table.
Comments
Post a Comment