Updated on 2025-06-30 GMT+08:00

Capturing Change Data

Scenarios

You can use a stored procedure to enable or disable the change data capture (CDC) function for a specified database. Change data capture can record the insertion, update, and deletion activities of an enabled table, and provide detailed change information using an easy-to-use relational format.

Only RDS for SQL Server enterprise editions, RDS for SQL Server 2016 Standard Edition, and later standard editions support change data capture.

For more information about change data capture, see the official documents.

Prerequisites

  • An RDS for SQL Server DB instance has been connected. For details about how to connect to a DB instance, see Connecting to a DB Instance.
  • The stored procedure must be executed by a user who has the [CREATE ANY DATABASE] permission. If a user who does not have this permission attempts to execute the stored procedure, the system displays the following information:
    Database restores can only be performed by database logins with [CREATE ANY DATABASE] permissions.

Constraints

  • The change data capture function cannot be enabled or disabled for system databases. If you attempt to enable or disable change data capture for a system database, the system displays the following information:
    CDC can not open on system database and [rdsadmin].
  • The change data capture operation can only be 1 or 0. If other operations are performed, the system displays the following information:
    @dbAction just support 1:open 0:close

Procedure

  • Enable or disable database-level CDC.
    EXEC msdb.dbo.rds_cdc_db '@DBName', @DBAction;
    • @DBName: Name of the target database.
    • @DBAction: Operation type. Entering the value 1 indicates enabling CDC, and entering the value 0 indicates disabling CDC.

    For example, to enable CDC for testDB_1, run the following command:

    EXEC msdb.dbo.rds_cdc_db 'testDB_1', 1;
  • Enable table-level CDC.
    EXEC sys.sp_cdc_enable_table 
             @source_schema = 'dbo',
             @source_name = 'testtable',
             @role_name = NULL
    • @source_schema: Schema name.
    • @source_name: Table name.
    • @role_name: used to restrict the access permission on the modified data. If you enter NULL, the access permission is not restricted.
  • Disable table-level CDC.
    EXEC sys.sp_cdc_disable_table 
             @source_schema = 'dbo',
             @source_name = 'testtable',
             @capture_instance ='all'
    • @source_schema: Schema name.
    • @source_name: Table name.
    • @capture_instance: Name of the instance for which CDC is disabled. If you enter all, CDC will be disabled for all instances.
  • Check whether table-level CDC is enabled.
    use [testdb]
    SELECT is_tracked_by_cdc FROM sys.tables WHERE name='table_name'
    • If the value 1 is returned, table-level CDC is enabled.
    • If the value 0 is returned, table-level CDC is disabled.