Today I'm looking at one of my favorite SQL Server 2008 features, Change Data Capture (CDC from here). CDC collects insert, update, and delete activity against a SQL Server table and stores the details of the changes in a relational format. CDC will be available in the Enterprise, Developer, and Evaluation editions.
The steps for configuring CDC on your instance require sysadmin fixed server role permissions.
First, we need to enable CDC on our database. To do so we issue the following call to sp_cdc_enable_db:
USE ExampleCode;
GO
--Activate change data capture at database level
EXECUTE sys.sp_cdc_enable_db;
To check any database for CDC activation we can query the sys.databases catalog view.
--Check CDC activation for databases
SELECT name, is_cdc_enabled
FROM sys.databases
WHERE [name] NOT IN ('master', 'tempdb', 'model', 'msdb');
Notice that I'm excluding the system databases -- these are not supported for CDC.
In my database I have an existing table named Customer with the following schema:
CREATE TABLE dbo.Customer
(
CustomerID INT IDENTITY(1,1) NOT NULL
,CustomerName VARCHAR(100) NOT NULL
,DoingBusinessAs VARCHAR(100) SPARSE NULL
,CustomerTypeID SMALLINT NOT NULL
,CreatedOn DATE NOT NULL
);
We enable CDC on the Customer table with the following call to sp_cdc_enable_table:
--Activate change data capture at table level
EXECUTE sys.sp_cdc_enable_table
@source_schema = 'dbo'
,@source_name = 'Customer'
,@role_name = 'cdc_examplecode';
To check any table for CDC activation we can query the sys.tables catalog view.
--Check CDC activation for our table
SELECT name, is_tracked_by_cdc
FROM sys.tables
WHERE name = 'Customer';
If we have a look around our database we will notice several objects have been created. First, we will find a list of new System Tables with the cdc schema.
Notice that a table has been created matching the schema of our referenced table, Customer. If you open the Security tab you will also notice a new role under Database Roles, cdc_examplecode (which we referenced in our procedure call).
Now the real fun begins. We will insert and update some data in our Customer table, as we would do in a normal database application.
--Insert some data
INSERT dbo.Customer (CustomerName, DoingBusinessAs, CustomerTypeID, CreatedOn)
VALUES ('ExampleCode', 'Blog Example Customer', 1, GETDATE());
--Update some data
UPDATE dbo.Customer
SET DoingBusinessAs = 'Updated Blog Example Customer'
WHERE CustomerName = 'ExampleCode';
To view the effects of our DML changes we can run the following query:
--View the effects
SELECT CASE
WHEN __$operation = 1 THEN 'Delete'
WHEN __$operation = 2 THEN 'Insert'
WHEN __$operation = 3 THEN 'Update (before image)'
WHEN __$operation = 4 THEN 'Update (after image)'
END AS cdcOperation
,CustomerID
,CustomerName
,DoingBusinessAs
,CustomerTypeID
,CreatedOn
FROM cdc.dbo_Customer_CT;
This will return all statements that have been issued against the Customer table with a "before" and "after" image for any updates. Sweet!
For those of you that might be worried about the potential cost of keeping this data I have good news there as well. By default, a cleanup process is automatically enabled that retains change data for three days. This is configurable, allowing you to balance the business need versus storage cost proposition.
I love the implementation of this feature. No longer am I resorting to triggers for keeping up with changes in my database. While triggers are certainly easy to use, they have additional consequences on development that just don't exist with CDC.
Another complimentary feature I can see, introduced in SQL Server 2005, would be a DDL trigger to make sure tables are not altered without the proper additions to CDC. Any columns added to a table after issuing the sp_cdc_enable_table stored procedure call are ignored in the tracking table. Columns that are dropped will remain in the tracking table with NULL values.
At the risk of sounding like a giddy little kid, SQL Server 2008 has really got me excited. I think this will be a huge release with some very compelling functionality.
For further BOL reading, check the topic How Change Data Capture Works.
f5c11d76-55e0-486f-b410-40ca37614039|0|.0