Introduction
Every developer who has worked with SQL SERVER sooner or later has come across this problem, where he or she has to take a copy of the row/s before performing any DML operations, and the table in which it is copied is generally marked as ‘tablename_history’ or ‘tablename_backup’ and this is achieved by writing an insert query in a stored procedure or trigger whichever found appropriate.
Recently I stumbled upon a system function in the SQL SERVER called Change Data Capture (CDC in short), which does the above function(if enabled) asynchronously by default and is supported by all versions higher than SQL Server 2008.
Enabling Change Data Capture
To implement CDC we first need to enable CDC on a database, this is done by executing the stored procedure "sys.sp_cdc_enable_db" as given below.
- -- To Enable CDC
- USE [CDC_TEST]
- GO
- EXEC sys.sp_cdc_enable_db
- GO
Now to enable CDC on the table, we need to do the stored procedure "sys.sp_cdc_enable_table" with its input parameters as given below
- USE [CDC_TEST]
- EXEC sys.sp_cdc_enable_table
- @source_schema = 'dbo', -- Is the name of the schema to which the source table belongs.
- @source_name = 'Customer', -- Is the name of the source table on which to enable change data capture
- @role_name = NULL -- Is the name of the database role used to gate access to change data, we can mention null if we want all the users having access to the database to view the CDC data
Once the stored procedure executes successfully some table with schema "cdc" is generated under the System Tables folder.

The tables include the following
- cdc.captured_columns table that contains the list of captured columns
- cdc.change_tables table that contains the list of tables that are enabled for capture
- cdc.ddl_history table that records the history of all the DDL changes since capture data enabled
- cdc.index_columns table that contains all the indexes that are associated with change table
- cdc.lsn_time_mapping table that is used to map the LSN number with the time and finally one change table for each CDC enabled table that is used to capture the DML changes on the source table
- cdc.dbo_Customer_CT table that contains the actual data before any DML operation is executed and some additional metadata like the operation, affected columns count, etc. The name of the table may vary depending on the name of the primary table on which the CDC is applied, but in general, it will be "NameOfSchema_TableName_CT" hence the name "dbo_Customer_CT".
With the tables, two SQL Agent Jobs are also created for given below
- cdc.CDC_TEST_capture job is responsible to push the DML changes into change tables
- cdc.CDC_TEST_cleanup job is responsible to clean up the records from the change tables. This job is created automatically by SQL Server to minimize the number of records in the change tables, failing this job execution will be resulting in a larger change table.
Detect Changes
So now that we have implemented CDC on the database and table, let's perform some DML operations given below


Join the conversation! Your thoughts help the community grow.