SQL: Adding time to Change Data Capture (CDC) Events
Several times now on forums, I’ve seen questions about Change Data Capture (CDC). People like the way they can retrieve details about changes that have occurred (often to trickle-feed into a data warehouse), but they are puzzled why CDC doesn’t tell them when the event occurred. There’s an easy fix for that.
Let’s start by doing a quick CDC setup:
DROP DATABASE IF EXISTS CDC;
GO
CREATE DATABASE CDC;
GO
USE CDC;
GO
CREATE TABLE dbo.NewEmployees
(
NewEmployeeID int IDENTITY(1,1) PRIMARY KEY CLUSTERED,
FullName nvarchar(100)
);
GO
-- note new last few columns in:
SELECT * FROM sys.databases WHERE name = N'CDC';
-- enable cdc at the database level
EXEC sys.sp_cdc_enable_db;
-- enable cdc for the table - requires SQL Agent running
EXEC sys.sp_cdc_enable_table
@source_schema = 'dbo',
@source_name = 'NewEmployees',
@supports_net_changes = 1,
@role_name = NULL;
-- review the state of CDC
SELECT name, is_tracked_by_cdc FROM sys.tables;
GO
And let’s then add three rows of data, then change one of them:
2020-05-14

