Change Data Capture (CDC) is a powerful feature in SQL Server that allows you to track changes made to your database tables. However, when it comes to Azure SQL Database, CDC works a bit differently. In this article, we will explore how to use CDC in Azure SQL Database and understand its functionality.
What is Change Data Capture (CDC)?
CDC is a feature that records insert, delete, and update activity on a database table. It creates a mirror table with additional columns to track the changes made to the database. For each insert and delete statement, CDC writes one record into the mirror table. For each update statement, it writes two records containing the data before and after the change.
In on-prem SQL Server, CDC uses SQL Server Agent to perform these tracking and recording tasks. However, Azure SQL Database does not support SQL Server Agent. Instead, it uses a change data capture scheduler to invoke the necessary stored procedures for capturing and cleaning up CDC tables.
Enabling CDC in Azure SQL Database
To enable CDC in Azure SQL Database, you need to follow these steps:
- Check if CDC is enabled for your database by running the following query:
SELECT
name,
CASE is_cdc_enabled
WHEN 0 THEN 'CDC not enabled'
WHEN 1 THEN 'CDC enabled'
ELSE 'Invalid value'
END AS CDCstats
FROM sys.databases
WHERE name='YourDatabaseName'
If CDC is not enabled, you can proceed to the next step.
- Enable CDC at the database level by executing the stored procedure
sys.sp_cdc_enable_db
. Make sure you are using thedb_owner
account security context and that your Azure SQL Database is in a service tier that supports CDC.
EXEC sys.sp_cdc_enable_db
- Enable CDC for specific tables using the stored procedure
sys.sp_cdc_enable_table
. Specify the schema and table name, and any other optional parameters.
EXEC sys.sp_cdc_enable_table
@source_schema = N'YourSchema',
@source_name = N'YourTableName',
@role_name = NULL,
@filegroup_name = NULL,
@supports_net_changes = 0
Once CDC is enabled for your Azure SQL Database and tables, you can start tracking changes and accessing the captured data.
Viewing Captured Data
After enabling CDC and performing insert, update, and delete transactions on your tables, you can view the captured data using the change tracking tables created by CDC.
For example, if you have a table named Employee
, you can use the following query to view the captured data:
SELECT * FROM cdc.dbo_Employee_CT
This query will return the inserted, deleted, and updated values in the Employee
table.
You can also use table-valued functions (TVFs) provided by CDC to access the change data. These functions include sys.fn_cdc_get_min_lsn
, sys.fn_cdc_get_max_lsn
, sys.fn_cdc_map_time_to_lsn
, sys.fn_cdc_map_lsn_to_time
, sys.fn_cdc_increment_lsn
, and sys.fn_cdc_decrement_lsn
.
Disabling CDC
If you no longer need to track changes using CDC, you can disable it for specific tables or the entire Azure SQL Database.
To disable CDC for a table, use the stored procedure sys.sp_cdc_disable_table
and specify the source table schema, name, and capture instance.
EXEC sys.sp_cdc_disable_table
@source_schema = N'YourSchema',
@source_name = N'YourTableName',
@capture_instance = N'YourCaptureInstance'
To disable CDC for the entire Azure SQL Database, execute the stored procedure sys.sp_cdc_disable_db
.
EXEC sys.sp_cdc_disable_db
Once CDC is disabled, the change tracking tables and other CDC-related objects will no longer be available.
Conclusion
Change Data Capture is a valuable feature in SQL Server that allows you to track changes made to your database tables. In Azure SQL Database, CDC works differently, using a change data capture scheduler instead of SQL Server Agent. By following the steps outlined in this article, you can enable CDC for your Azure SQL Database and start tracking changes efficiently.
Remember to check the CDC support for your Azure SQL Database service tier and ensure that you have the necessary permissions to enable and disable CDC.
Thank you for reading!
Article Last Updated: 2022-05-02