This article is written based on the SQL Server 2008 – Nov CTP.
firstly we have database and we want capture changes done on this data base and it's tables ,who edit in which and when,it is become easy with sql2008 let's start.
1- Enable Change Data Capture, a new schema with a bunch of objects will be created.
USE [MyDataBase]
GO
EXEC sys.sp_cdc_enable_db_change_data_capture
GO
The following CDC tables are created under the CDC schema, as shown below.
cdc.captured_columns
cdc.change_tables
cdc.ddl_history
cdc.index_columns
cdc.lsn_time_mapping
When you query these tables, you will see only zero number of rows.
2-In order to track changes on the table, we need to enable the Change Data Capture feature on the table as well.
USE [MyDataBase]
GO
EXEC sys.sp_cdc_enable_table_change_data_capture
@source_schema = 'dbo',
@source_name = 'MyTable',
@role_name = 'cdc_MyTable'
GO
we can easily understand that SQL Server Agent is a must to do the capture and cleanup. We can see these jobs are actually created as SQL Server Scheduled jobs.
![]()
3-You can see that the CDC schema stores metadata information about which tables and columns are being tracked by Change Data Capture. It also stores information about what Index the table that has been tracked has.
4-We can check to see if Change Data Capture is enabled on a database by
SELECT is_cdc_enabled FROM sys.databases WHERE name = 'MyDataBase'
result '1' means enabled.'0' Disabled
5-We can check to see if Change Data Capture is enabled on a table, by
SELECT is_tracked_by_cdc FROM sys.tables WHERE name = 'MyTable'
6-Now let’s make some changes to table structure and see if Change Data Capture captures the changes. Execute the following query as shown below.
USE [MyDataBase]
GO
Alter Table MyTable add Address varchar(500)
GO
Alter Table MyTable add Salary money
GO
Alter Table MyTable add Bonus money
GO
7-Query the cdc table ddl_history as shown below.
select * from cdc.ddl_history
![]()
Conclusion
This article illustrated how to enable the new SQL Server Feature “Change Data Capture” on a database. In addition, it illustrated how to enable Change Data Capture on a table and how to keep track of Data Definition Language changes on a table. It also explained the CDC schema and changes happening in the objects of the CDC schema.
Next post ISA will be about how SQL Server tracks the data changes of the CDC enabled table.
wait for it
References
http://www.databasejournal.com/
Moayed Mohammed
software development Engineer
Mob:0102091804