Loading...

We've detected that your browser language is Chinese. Would you like to visit our Chinese website? [ Dismiss ]
By: Emma

What Is Change Data Capture (CDC) in SQL Server?

SQL Server Change Data Capture (CDC) is a built-in feature that records INSERT, UPDATE, and DELETE operations on database tables. It captures changes asynchronously from the transaction log and stores them in change tables, allowing applications to access incremental data without scanning entire tables.

For data engineers and DBAs, CDC in SQL Server provides an efficient way to support data synchronization, ETL workflows, and analytics by tracking only the data that has changed.

SQL Server CDC captures three main types of data changes:

  • Inserts: Records newly added rows.
  • Updates: Captures data before and after a row modification.
  • Deletes: Stores row values before they are removed.

For example, when an employee’s salary changes from $75,000 to $80,000, CDC records both the previous and updated values in a change table. This allows downstream systems to identify what changed and when the change occurred.

How Does Change Data Capture Work in SQL Server?

Understanding how SQL Server CDC works makes it easier to configure and manage. After CDC is enabled, data changes are captured asynchronously through a four-step process with minimal impact on normal database operations.

how does change data capture work in sql server

Step 1. SQL Server Records Changes in the Transaction Log

Whenever data is inserted, updated, or deleted, SQL Server records the operation in the transaction log. Instead of monitoring user queries or using triggers, CDC reads these existing log records to capture data changes.

Step 2. The CDC Capture Job Reads the Transaction Log

After CDC is enabled, SQL Server creates a capture job that continuously scans the transaction log for committed changes. Running in the background, it processes changes asynchronously with minimal impact on write performance.

Step 3. SQL Server Stores Changes in CDC Change Tables

The capture job writes the detected changes to system-managed change tables in the cdc schema, such as cdc.dbo_TableName_CT. Each record includes the changed data along with metadata like the Log Sequence Number (LSN) and operation type.

Step 4. Applications Read Incremental Changes

Applications, ETL tools, and replication services can retrieve incremental changes from the CDC change tables. Microsoft recommends using the built-in CDC functions, such as fn_cdc_get_all_changes and fn_cdc_get_net_changes, instead of querying the change tables directly.

How to Enable Change Data Capture in SQL Server

Enabling SQL Server Change Data Capture (CDC) involves three steps: verifying the prerequisites, enabling CDC at the database level, and then enabling it for the tables you want to track.

Prerequisites Before Enabling CDC

Before enabling CDC, make sure your environment meets these requirements:

  • SQL Server Edition: CDC is supported on Developer, Enterprise, and Standard editions (SQL Server 2016 SP1 and later). It is not available in SQL Server Express.
  • SQL Server Agent: The capture and cleanup jobs depend on SQL Server Agent. Ensure the service is running.
  • Permissions: You must have db_owner permissions on the target database or sysadmin server-level privileges.
  • Primary Key (Recommended): A primary key or unique index is required if you want to use the net changes function.

Enable CDC at the Database Level

Before you can track table changes, enable CDC for the database.

sql
USE YourDatabaseName;
    GO
    
    -- Check whether CDC is enabled
    SELECT name, is_cdc_enabled
    FROM sys.databases
    WHERE name = DB_NAME();
    
    -- Enable CDC
    EXEC sys.sp_cdc_enable_db;
    GO    

Enable CDC for a Table

After enabling CDC for the database, you can enable it for individual tables.

USE YourDatabaseName;

sql
USE YourDatabaseName;
    GO
    
    -- Create a sample table if it does not exist
    IF OBJECT_ID('dbo.Employees', 'U') IS NULL
    BEGIN
        CREATE TABLE dbo.Employees (
            EmployeeID INT IDENTITY(1,1) PRIMARY KEY,
            FirstName VARCHAR(50),
            LastName VARCHAR(50),
            Salary DECIMAL(18,2),
            LastModified DATETIME DEFAULT GETDATE()
        );
    END;
    GO
    
    -- Enable CDC on the table
    EXEC sys.sp_cdc_enable_table
        @source_schema = N'dbo',
        @source_name = N'Employees',
        @role_name = NULL,
        @supports_net_changes = 1;
    GO    

Verify That CDC Is Working Correctly

Run the following queries to verify that CDC has been enabled successfully.

sql
-- Verify that the table is tracked by CDC
    SELECT name, is_tracked_by_cdc
    FROM sys.tables
    WHERE name = 'Employees';
    
    -- Verify that the capture and cleanup jobs exist
    EXEC sys.sp_cdc_help_jobs;    

If CDC is enabled successfully, is_tracked_by_cdc returns 1, and both the capture and cleanup jobs are listed.

How to Read Change Data in SQL Server CDC

SQL Server provides built-in functions for querying captured changes instead of accessing the CDC change tables directly.

Understanding Log Sequence Numbers (LSNs)

Each transaction in SQL Server is identified by a Log Sequence Number (LSN). To query changes within a specific time range, first convert the start and end timestamps into LSN values.

DECLARE @from_lsn BINARY(10), @to_lsn BINARY(10);

    -- Map a time range to LSN values
    SET @from_lsn = sys.fn_cdc_map_time_to_lsn(
        'smallest greater than or equal',
        '2026-07-15 08:00:00'
    );
    
    SET @to_lsn = sys.fn_cdc_map_time_to_lsn(
        'largest less than or equal',
        '2026-07-15 17:00:00'
    );    

Using fn_cdc_get_all_changes

Use fn_cdc_get_all_changes to return every INSERT, UPDATE, and DELETE operation within an LSN range. This function is commonly used for auditing and detailed change tracking.

sql
SELECT *
FROM cdc.fn_cdc_get_all_changes_dbo_Employees(
    @from_lsn,
    @to_lsn,
    'all'
);

Using fn_cdc_get_net_changes

Use fn_cdc_get_net_changes when you only need the final state of each modified row within the selected LSN range.

sql
SELECT *
    FROM cdc.fn_cdc_get_net_changes_dbo_Employees(
        @from_lsn,
        @to_lsn,
        'all'
    );    

Understanding CDC Operation Codes

The __$operation column identifies the type of data change returned by the CDC functions.

Operation Code Operation Type Description
1 Delete The row was deleted.
2 Insert A new row was inserted.
3 Update (Before) The row values before the update.
4 Update (After) The row values after the update.

SQL Server CDC vs Change Tracking

SQL Server provides two built-in features for tracking data changes: Change Data Capture (CDC) and Change Tracking (CT). Although both record data modifications, they serve different purposes. CDC captures detailed change history, while Change Tracking simply identifies which rows have changed.

Feature Change Data Capture (CDC) Change Tracking (CT)
Primary Purpose Captures detailed change history for auditing and ETL. Tracks changed rows for data synchronization.
Captured Data Stores the complete changed row. Stores only the primary key and change information.
Old and New Values Captures both before and after values. Does not store historical values.
Performance Impact Runs asynchronously with minimal impact on transactions. Tracks changes during transactions with low overhead.
Storage Higher storage usage. Lower storage usage.
Best For ETL, analytics, auditing, and data replication. Application and client synchronization.
Audit Capability Supports complete change history. Does not support historical auditing.

In short, choose CDC when you need detailed change history, and choose Change Tracking when you only need to identify changed rows efficiently.

When Should You Choose CDC?

Choose SQL Server CDC when you need a complete history of data changes rather than just the latest state. It is well suited for ETL pipelines, data warehouses, real-time analytics, and streaming platforms where every INSERT, UPDATE, and DELETE needs to be captured.

When Is Change Tracking a Better Choice?

Choose Change Tracking when you only need to know which rows have changed since the last synchronization. Because it stores less information than CDC, it uses less storage and is better suited for lightweight synchronization between applications and SQL Server.

Unlike CDC, Change Tracking does not preserve historical updates. If a row is updated multiple times before synchronization, only its latest state is available.

Common Limitations of SQL Server CDC

Although SQL Server CDC is a powerful feature, it has several limitations that should be considered before using it in production.

SQL Server Agent Dependency

CDC relies on SQL Server Agent to run the capture and cleanup jobs. If the service stops, changes are no longer processed, which can prevent the transaction log from being truncated and cause it to grow over time.

CDC Storage Growth

CDC stores copies of changed data in system change tables under the cdc schema. In databases with frequent updates or large columns, these tables can grow quickly, so sufficient storage should be planned.

Data Retention Period

By default, captured changes are retained for 72 hours (3 days) before the cleanup job removes them.

If downstream applications or ETL processes do not read the changes within this period, the data is permanently deleted. You can increase the retention period, but doing so also increases storage usage.

Unsupported Operations

Some schema changes are not handled automatically by CDC and may require additional configuration.

  • Renaming columns
  • Changing column data types or sizes
  • Memory-optimized (In-Memory OLTP) tables

Permission Requirements

Enabling and managing CDC requires sysadmin or db_owner permissions. Access to captured data can be restricted by configuring a gating role when CDC is enabled.

SQL Server Version and Edition Considerations

CDC is supported in SQL Server Developer, Enterprise, and Standard editions (SQL Server 2016 SP1 and later). It is not available in SQL Server Express.

From Change Data Capture to Real-Time Data Replication

CDC is built for tracking changes inside a single database, not for keeping two databases in sync in real time. Its capture job runs on a schedule, which means there is always some delay between a change happening and that change showing up in the change tables. For auditing or ETL pipelines, that delay is fine. For disaster recovery, cross-database sync, or feeding a reporting system that needs fresh data, it becomes a limitation.

This is where a dedicated database replication tool like i2Stream fits in. Instead of periodically capturing changes into tables within the same database, i2Stream reads the transaction log directly and streams those changes to a target database in real time, whether that target sits on the same platform or a different one entirely.

i2Stream comes with several features that address the exact gaps CDC leaves open:

  • Millisecond-level sync with transaction-level consistency: i2Stream parses database logs directly to replicate changes with millisecond latency, even under high concurrency, so target databases stay current instead of waiting on a scheduled capture job.
  • Cross-platform and cross-version replication: Unlike CDC, which is SQL Server-specific, i2Stream supports 40+ database environments, including SQL Server, Oracle, MySQL, and PostgreSQL, so you can replicate to a different database engine or version without rebuilding your change-tracking logic.
  • Integrated DDL and DML sync: i2Stream captures schema changes alongside data changes, which avoids the manual schema-tracking overhead that comes with managing CDC-enabled tables individually.
  • Agentless deployment: i2Stream runs without installing software on the production database, so there’s no added load on the source system, a common concern when CDC’s capture and cleanup jobs compete with production workloads.
  • Point-in-time recovery from a specific SCN: For teams that need recovery precision beyond what CDC’s retention window offers, i2Stream supports recovery to a specific point without requiring a full database mirror.

If your use case is closer to keeping two databases continuously synchronized, whether for disaster recovery, migration, or automatic failover, i2Stream extends what CDC starts, turning periodic change capture into continuous, cross-platform replication.

Info2Soft also offers other solutions for data resilience depending on what you’re protecting. i2Backup handles centralized backup across databases, VMs, and physical servers, while i2Availability provides real-time replication built specifically for high-availability failover scenarios.

FREE Trial for 60-Day

SQL Server CDC Best Practices

Following a few best practices can help improve the performance, reliability, and maintainability of SQL Server CDC in production environments.

  • Enable CDC Only for Required Tables: Enable CDC only for the tables that require change tracking. Avoid enabling it on temporary or high-churn tables unless necessary. If you only need to capture specific columns, use the @captured_column_list parameter to reduce the size of the change tables.
  • Monitor Capture and Cleanup Jobs: CDC depends on the capture and cleanup jobs to process and remove change data. Monitor these SQL Server Agent jobs and configure alerts so that failures can be detected and resolved quickly.
  • Configure Appropriate Retention Periods: By default, CDC retains captured changes for 4320 minutes (3 days). Adjust the retention period based on how frequently downstream applications consume change data.
sql
EXEC sys.sp_cdc_change_job
    @job_type = N'cleanup',
    @retention = 5760;
GO

EXEC sys.sp_cdc_stop_job @job_type = N'cleanup';
EXEC sys.sp_cdc_start_job @job_type = N'cleanup';
GO
  • Avoid Direct Queries Against Change Tables: Although CDC change tables can be queried directly, Microsoft recommends using the built-in functions, such as fn_cdc_get_all_changes and fn_cdc_get_net_changes. These functions provide a consistent way to retrieve changes within a specified LSN range.
  • Archive Historical Change Data: If long-term retention is required for auditing or compliance, archive captured changes to a separate history database or data lake instead of extending the live CDC retention period. This helps control storage growth and maintain database performance.
  • Monitor Capture Latency Regularly: In write-intensive environments, monitor the capture job regularly to ensure it keeps up with incoming transactions.
sql
SELECT
    session_id,
    start_time,
    end_time,
    duration,
    scan_phase,
    log_record_count,
    latency
FROM sys.dm_cdc_log_scan_sessions;

If capture latency continues to increase, consider adjusting the @maxtrans or @maxscans settings for the capture job to process more transactions during each scan.

Tip: Review your CDC configuration regularly to ensure retention settings, storage usage, and capture performance continue to meet your workload requirements.

Conclusion

Change Data Capture gives SQL Server a built-in way to track row-level changes for auditing, ETL, and incremental sync, without building custom triggers or timestamp logic. Enabling it takes just a few steps, but getting the most out of it means understanding its retention limits, storage impact, and how it differs from Change Tracking.

If your needs go beyond capturing changes and into keeping databases continuously synchronized across platforms, a real-time replication tool like Info2soft’s i2Stream is worth exploring next.

Emma is the bridge between complex engineering and the people who need it. As a content creator at Info2soft, she spends her days translating "tech-speak" into clear, actionable stories about data resilience. She’s not just documenting software; she's uncovering how data replication and recovery actually change the way businesses run.

More Related Articles

Table of Contents:
Stay Updated on Latest Tips
Subscribe to our newsletter for the latest insights, news, exclusive content. You can unsubscribe at any time.
Subscribe
Ready to Enhance Business Data Security?
Start a 60-day free trial or view demo to see how Info2soft protects enterprise data.
Please fill out the form and submit it, our customer service representative will contact you soon.
By submitting this form, I confirm that I have read and agree to the Privacy Notice.
{{ isSubmitting ? 'Submitting...' : 'Submit' }}