Problem
I use the SQL Server Database Project for developing, maintaining, and publishing/deploying my databases. I need to implement Change Tracking in one of my databases. How can I do that in the SQL Server Database Project?
Solution
The SQL Server Database Project can be used to manage the development and deployment of a SQL Server database. You can download the SQL Server Data Tools for Visual Studio 2012 here.
The SQL Server Database Project has a “publishing” capability which essentially allows you to generate a T-SQL script to deploy your database to a target server. As part of the publishing capability, you can use a Post-Deployment script which is T-SQL code that gets included in the script that is generated when you publish your database. The T-SQL code in the Post-Deployment script gets executed after the database objects have been created; e.g. tables, views, stored procedures, etc. A key point to note about the Post-Deployment script is that it is executed every time you publish your database, even if you are only publishing updates rather than the full database.
In this tip I will start off with how to setup change tracking with just T-SQL code, then perform the following in a SQL Server database project:
- Enable change tracking for the database
- Enable change tracking for a table in the Post-Deployment script
- Setup a publishing profile
- Generate a T-SQL script to publish the database
I am going to assume that you are familiar with the SQL Server database project. If you haven’t used it before or need a refresher, take a look at the Creating a New Database Project Tutorial. Although it’s based on the 2010 version, it will get you up-to-speed quickly.
Setup SQL Server Change Tracking with T-SQL
Change Tracking is a feature that when enabled will keep track of the rows that are inserted, updated or deleted in a table. Change Tracking needs to be enabled at the database level and also enabled on each table that you want to track. If you would like more details on Change tracking, take a look at the tip Using Change Tracking in SQL Server 2008.
Assuming we have a database named MSSQLTIPS_CT_DEMO, run the following T-SQL code to enable change tracking on the database (retain change tracking data for 5 days and automatically clean it up):
ALTER DATABASE MSSQLTIPS_CT_DEMO SET CHANGE_TRACKING = ON (CHANGE_RETENTION = 5 DAYS, AUTO_CLEANUP = ON); GO
To see the list of databases that have change tracking enabled, run the following T-SQL code:
SELECT DB_NAME(database_id) [DATABASE_NAME] FROM sys.change_tracking_databases; GO
Assuming we have a table named CUSTOMER, run the following T-SQL code to enable change tracking on the CUSTOMER table (just track the rows that have changed without the column change detail):
ALTER TABLE dbo.CUSTOMER ENABLE CHANGE_TRACKING WITH (TRACK_COLUMNS_UPDATED = OFF); GO
To see the list of tables that have change tracking enabled, run the following T-SQL command:
SELECT OBJECT_NAME(object_id) [TABLE_NAME] FROM sys.change_tracking_tables; GO
Perform an insert to generate some change tracking data:
INSERT dbo.CUSTOMER ([NAME]) VALUES ('ABC COMPANY'); GO
Query the change tracking data by running the following T-SQL code:
SELECT ct.ID, ct.SYS_CHANGE_OPERATION, c.[NAME] FROM CHANGETABLE(CHANGES dbo.CUSTOMER, 0) ct JOIN dbo.CUSTOMER c ON c.[ID] = ct.[ID]; GO