Jump to Content
Developers & Practitioners

Track changes in SQL Server on Google Cloud using Change Data Capture

April 23, 2021
Jonathan Simon

Developer Programs Engineer, Google Cloud

Knowing the exact changes made to a SQL Server database on Google Cloud is now possible with the release of support for Change Data Capture (CDC) in Cloud SQL for SQL Server. The following SQL Server versions now support CDC in Cloud SQL:

  • SQL Server 2017 Standard

  • SQL Server 2017 Enterprise

This blog post will walk through the steps required to enable CDC along with an example query to view captured changes.

First you will need to create a SQL Server instance in Cloud SQL which you can do from the Google Cloud Console.

For the full details on creating the SQL Server instance and connecting to it using Azure Data Studio see my previous post: Try out SQL Server on Google Cloud at your own pace

Create a database and a table

We'll start by creating a new database on our SQL Server instance. With Azure Data Studio connected to your SQL Server instance, right click the server in Azure Data Studio and select “New Query”.

https://storage.googleapis.com/gweb-cloudblog-publish/images/image2_1nCfcw6.max-900x900.png

Enter the following SQL statement to create a new database to work with:

Loading...

and click the “Run” button.

https://storage.googleapis.com/gweb-cloudblog-publish/images/image5_d7sKVk5.max-1200x1200.png

Next we'll create a new table named "leaderboard". Enter the following query:

Loading...

and click the “Run” button.

https://storage.googleapis.com/gweb-cloudblog-publish/images/image6_c47ifB1.max-1200x1200.png

Enable Change Data Capture (CDC) on database and table

Okay! Now it's time to perform the main focus of this post, enabling change data capture. This will take two steps, enabling CDC on the database and then on the table. First enable CDC on the database named "demo" by entering the following SQL statement:

EXEC msdb.[dbo].[gcloudsql_cdc_enable_db] 'demo'

and click the “Run” button.

https://storage.googleapis.com/gweb-cloudblog-publish/images/image4_Tj6nV6G.max-900x900.png

Then enter the following SQL statement to enable CDC on the table named "leaderboard":

Loading...

and click the “Run” button.

https://storage.googleapis.com/gweb-cloudblog-publish/images/image7_ak3PtcG.max-1200x1200.png

Confirm that CDC is working as expected


Alright! Now that we've got CDC enabled on our database and table let's run a couple of queries to insert some data and then we can test out a CDC query to confirm that changes to our table are being tracked via CDC as expected. CDC will capture all change operations made to the table like execution of INSERT, UPDATE or DELETE statements. We'll just run a couple of INSERT statements to demonstrate how this operation is captured by CDC.

Enter the following SQL statements to insert two records into the table named "leaderboard":

Loading...

and click the “Run” button.

https://storage.googleapis.com/gweb-cloudblog-publish/images/image3_hQfq9IK.max-1100x1100.png

Now that we've made some changes to the table let's wrap things up by running a query that will get all the results captured by CDC in a special change table named as "<schema>_<table_name>_CT". In this case for the table we created it's named "dbo_leaderboard_CT".

Enter the following query:


SELECT * FROM cdc.dbo_leaderboard_CT


and click the "Run" button. Voila! The INSERT operations have been captured along with the CDC metadata that can be used for tracking exactly when and what changes were made to your database tables in SQL Server.

https://storage.googleapis.com/gweb-cloudblog-publish/images/image1_IoIILvb.max-800x800.png

Now, read about the CDC support in Cloud SQL and create a SQL Server instance in Cloud SQL which you can do from the Google Cloud Console.

Posted in