Skip to main content

Change Data Capture in SQL Server


Change Data Capture (CDC) captures the data of insert, update and delete activity. When you insert or delete the data in the table it maintains a record for the same data. When you update the data it maintains records for before updating the data and after updating the data.

To understand the change data capture we go through the following process.
Process
process

Step 1: Create DB 
  1. CREATE DATABASE CDC_DEMO  
  2. GO  
Step 2: Create Table
Create one table in the preceding database.
Execute the following query and the "CDC_DEMO_TABLE1" table is created. 
  1. USE CDC_DEMO  
  2. GO  
  3.   
  4. CREATE TABLE CDC_DEMO_TABLE1  
  5. (   
  6.     ID      INT         IDENTITY(1,1) PRIMARY KEY,  
  7.     Name        VARCHAR(50)     NOT NULL,  
  8.     Age     INT         NOT NULL,  
  9. );  
  10. GO  
You can check the table in the Object Explorer. 
object explorer

Step 3: Insert Rows
Insert some rows into the table "CDC_DEMO_TABLE1".
Here we inserted two rows into the table. 
  1. USE CDC_DEMO  
  2. GO  
  3.   
  4. INSERT INTO CDC_DEMO_TABLE1 (Name,Age) VALUES ('Akshay',34)  
  5. GO  
  6. INSERT INTO CDC_DEMO_TABLE1 (Name,Age) VALUES ('Kaushal',38)  
  7. GO  
insert rows

Step 4: Enable CDC on DB
We have a database, table and some rows in the table, now we need to enable CDC on the database.
  1. Execute the following query and it will show whether CDC is enabled or not for the database.
    1. USE CDC_DEMO  
    2. GO  
    3.   
    4. SELECT name, database_id, is_cdc_enabled    
    5. FROM SYS.DATABASES  
    6. WHERE name = 'CDC_DEMO'  
    cdc enabled

    "is_cdc_enabled" has the value "0", that means it is not enabled for the database.
  2. Execute the following query to enable CDC on the database. We need to execute the "sys.sp_cdc_enable_db" Stored Procedure to enable CDC on the database. It is necessary to execute it before we know any tables are enabled for the CDC.
    1. USE CDC_DEMO  
    2. GO  
    3. EXEC sys.sp_cdc_enable_db  
    4. GO  
    This will create some system tables.

    system tables
  3. Check again and verify that CDC is enabled on the database.
    1. USE CDC_DEMO  
    2. GO  
    3.   
    4. SELECT name, database_id, is_cdc_enabled    
    5. FROM SYS.DATABASES  
    6. WHERE name = 'CDC_DEMO'  
    is cdc enabled

    Now "is_cdc_enabled" has the value 1, in other words it is enabled.
Step 5: Enable CDC on Table
Enable CDC on the "CDC_DEMO_TABLE1" table.
  1. Before enabling CDC, we need to check whether it is enabled already or not. Execute the following query and we have a list of all tables with CDC status.
    1. USE CDC_DEMO   
    2. GO   
    3. SELECT [name], is_tracked_by_cdc  FROM SYS.TABLES   
    4. GO    
    cdc demo table

    The value of "is_tracked_by_cdc" is "0" for the "CDC_DEMO_TABLE1" table, in other words CDC is not enabled for this table.
  2. Execute the following query to enable CDC on the table.
    1. USE CDC_DEMO;  
    2. GO  
    3. EXECUTE sys.sp_cdc_enable_table  
    4.   @source_schema = N'dbo'  
    5.   , @source_name = N'CDC_DEMO_TABLE1'  
    6.   , @role_name = NULL  
    7. GO  
    8.           
    We can check in the Object Explorer that one more table is created under the system tables, "cdc.dbo_CDC_DEMO_TABLE1_CT".

    cdc demo table ct
  3. Check again and verify that CDC is enabled on the table.
    1. USE CDC_DEMO   
    2. GO   
    3. SELECT [name], is_tracked_by_cdc  FROM SYS.TABLES   
    4. GO    
    check cdc enabled

    Now "is_tracked_by_cdc" has the value 1, that represents that CDC is enabled for the table.
Step 6: Insert Operation
We have enabled CDC for the database and table. Now let's check where SQL Server persists the change log when we insert the data in the table.
Execute the following query to insert one row into the table. 
  1. USE CDC_DEMO  
  2. GO  
  3.   
  4. INSERT INTO CDC_DEMO_TABLE1 (Name,Age) VALUES ('Jignesh',35)  
  5. GO  
Open the table "CDC_DEMO_TABLE1" and we can see that one row is inserted with the ID 3. 
id3 inserted

The change log is captured in the table "cdc.dbo_CDC_DEMO_TABLE1_CT". You can see the entire row that we have created. One more thing you can observe here is that the _$operation value is 2, in other words for Insert values.
demo table1 ct

Step 7: Update Operation
Now let's check by updating any of the rows in the table. Execute the following script that will update the value of the name field where id = 3.
  1. USE CDC_DEMO  
  2. GO  
  3.   
  4. UPDATE CDC_DEMO_TABLE1  
  5. SET Name = 'Jigi'  
  6. WHERE id = 3  
  7. GO  
Open the table and verify that the value is changed. 
name value changed

Open the "cdc.dbo_CDC_DEMO_TABLE1_CT" table and you can see that the update data is captured in two rows. One is with operation 3 and the other with operation 4. Operation value 3 means before updating and value 4 means after updating.
updated data

Step 8: Delete Operation
To check the captured data after the delete operation, execute the following script that deletes the record with id=3. 
  1. USE CDC_DEMO  
  2. GO  
  3.   
  4. DELETE FROM CDC_DEMO_TABLE1  
  5. WHERE id = 3  
  6. GO  
Open the table and verify that the record is deleted from the table.
record deleted

Open the "cdc.dbo_CDC_DEMO_TABLE1_CT" table and you can see that the deleted row is captured with operation value 1.
deleted row captured

We have seen change data capture for insert, update and delete operations and for those only one system table is used, "cdc.dbo_CDC_DEMO_TABLE1_CT". But there are more than six tables that were created when enabling CDC on the database. So let's see the schema and values for those tables:
  1. Cdc.captured_columns
    Provides the information of columns that are tracked for the changed data capture.

    columns info
  2. Cdc.change_tables
    Provides the information of the table. It shows the default value for "capture_instance" since we have not provided a parameter when enabling CDC on the table.

    capture instance
  3. Cdc.ddl_historyProvides the information for any schema changes. Currently this table doesn't have any value since we did not change any schema for the table. So let's change the schema and check the values. Execute the following query to change the schema for the table:
    1. USE CDC_DEMO  
    2. GO  
    3.   
    4. ALTER TABLE CDC_DEMO_TABLE1  
    5. ALTER COLUMN Name VARCHAR(100) NOT NULL  
    6. GO  
    We have changed the datatype from varchar(50) to varchar(100) for the name field.

    datatype changed

    Open the "cdc.ddl_history" table and we can see that the ddl_command is captured as in the following:

    ddl command captured
  4. Cdc.index_columnsProvides the information if any of the index column is changed.

     index column
  5. Cdc.Isn_time_mappingProvides the information about the start and end time for the operation done for changes.

    operations
  6. Cdc.systranschemasProvides the information for the schema changes.

    schema changes
Step 9: Disable CDC on Table
Execute the following query to disable CDC on the table.
  1. USE CDC_DEMO;  
  2. GO  
  3.   
  4. EXECUTE sys.sp_cdc_disable_table  
  5.     @source_schema = N'dbo',  
  6.     @source_name = N'CDC_DEMO_TABLE1',  
  7.     @capture_instance = N'dbo_CDC_DEMO_TABLE1'  
  8. GO  
We can observe in the Object Explorer that one table is removed under the system tables, "cdc.dbo_CDC_DEMO_TABLE1_CT". That means CDC is disabled for this table.
check cdc disabled

Step 10: Disable CDC on Database
Execute the following query to disable CDC on the database. 
  1. USE CDC_DEMO  
  2. GO  
  3. EXEC sys.sp_cdc_disable_db  
  4. GO   
We can observe in the Object Explorer that all the tables are removed under the system tables. That means CDC is disabled on the database. 
cdc disabled

Note : Change data capture is only available in the Enterprise, Developer, Enterprise Evaluation, and Standard editions.

Comments

Popular posts from this blog

PNR Status by web Scraping Method (ASP.NET) C#

To Get the PNR Status by web Scraping Method Steps to Execute the Function Step 1 : Add the below method in your Form and Pass the PNR Number arguement public string GetPNRStatus( string sPNR) { string URI = "http://www.indianrail.gov.in/cgi_bin/inet_pnrstat_cgi.cgi" ; string Parameters = Uri .EscapeUriString( "lccp_pnrno1=" +sPNR+ "&submitpnr=Get Status" ); System.Net. HttpWebRequest req = ( HttpWebRequest )System.Net. WebRequest .Create(URI); //HTTP POST Headers req.ContentType = "application/x-www-form-urlencoded" ; req.Host = "www.indianrail.gov.in" ; //You can use your own user-agent. req.UserAgent = "Mozilla/5.0 (compatible; MSIE 7.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0) DELL;Venue Pro" ; req.Headers.Add( HttpRequestHeader .AcceptLanguage, "en-us,en;q=0.5" ); req.Headers.Add( HttpRequestHeader .AcceptCharset, "ISO-8859-1,utf-8;q=
C# HttpClient tutorial C# HttpClient tutorial shows how to create HTTP requests with HttpClient in C#. In the examples, we create simple GET and POST requests. The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborative, hypermedia information systems. HTTP is the foundation of data communication for the World Wide Web. HttpClient  is a base class for sending HTTP requests and receiving HTTP responses from a resource identified by a URI. C# HttpClient status code HTTP response status codes indicate whether a specific HTTP request has been successfully completed. Responses are grouped in five classes: Informational responses (100–199) Successful responses (200–299) Redirects (300–399) Client errors (400–499) Server errors (500–599) Program.cs using System; using System.Net.Http; using System.Threading.Tasks; namespace HttpClientStatus { class Program { static async Task Main(string[] args) { using var client = new

SonarQube Configuration For .NET Core Web API

When multiple developers are working on the same project, it's good to have a code review. SonarQube is a tool through which we can evaluate our code. Here, for demo purposes, we are going to evaluate the web API which is built on .NET Core. Let's see step by step implementation. In order to run SonarQube, we need to install JAVA in our local system.   Refer to the below link to download JAVA installer and install JAVA. https://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html Configure the 'PATH' system variable under environment variables Go to Control Panel > System > Advanced System Settings, it will open the System Properties window. Click on the "Environment Variables" button. Click on the "View" button under User Variables. Give the variable name as 'JAVA_HOME'. The variable value will be your JDK path where you installed JAVA. Select path variable under system variable and click o