SQL Server: Change Data Capture
Introduction
CDC (Change Data Capture) captures data changes caused by DML (Data Manipulation Language) statements like inserts, updates, and deletions and makes it possible to archive data changes without any additional programming. These captured changed are stored in change tables that mirror the column structure of the original, tracked, user-created source tables with additional columns that include extra metadata. With CDC no changes are required to the existing source table schemas. Several database objects are provided (stored procedures, functions, and jobs) to handle the captured changes and integrate with external systems like a DWH (Data Warehouse).
Overview
All data changes caused by inserts, updates, and deletions are saved in the transaction log file
_log.ldf, which is used as a source for the change data capture process. This change data capture process is scheduled by the SQL Server Agent that executes the capture job cdc.
_capture. The capture job scans the transaction log every 5 seconds, processing a maximum of 1000 transactions (these numbers are based on default settings, which can be changed for performance optimalisation purposes). Take into account that there’s latency between the time that a change is committed to the source table and the time that the change appears within its associated change table as the capture process extracts data from the transaction log. Archiving changes causes huge amounts of data and therefore a cleanup job cdc.
_cleanup exists that removes change data from the change tables that is older than 3 days.
The CDC functionality is only supported by the Enterprise and Developer editions of SQL Server 2008 R2.
Setup
For using CDC functionality you have to go through the following steps.
Enable CDC for database
Before changes to any individual table within a database can be tracked change data capture must be explicitly enabled for the database, which you can do by executing the following T-SQL script:
EXEC sys.sp_cdc_enable_db
After executing this T-SQL script the following database objects are created:
Object
Description
Schema
Cdc
Tables
Several tables are created using the schema “cdc”<