Short answer: change data capture (CDC) reads the transaction log of a SQL Server or Azure SQL database and writes every insert, update and delete on the tables you choose into change tables. A load then asks for the changes between two log positions and gets each row with what happened to it, including deletes, which a watermark on a modified date never sees.
A watermark works until someone deletes a row or updates a table without touching its date column. CDC does not depend on the table's columns; it reads what the database itself recorded.
How does it compare?
| Way | Finds deletes | Keeps the values | Needs a column in the table |
|---|---|---|---|
| Watermark on a date | no | current row only | yes, a reliable modified date |
| Change tracking | yes | keys only, read the row yourself | no |
| Change data capture | yes | every change, before and after | no |
How do you turn it on and read it?
EXEC sys.sp_cdc_enable_db;
EXEC sys.sp_cdc_enable_table
@source_schema = N'dbo',
@source_name = N'Orders',
@role_name = NULL;
DECLARE @from binary(10) = sys.fn_cdc_get_min_lsn('dbo_Orders'),
@to binary(10) = sys.fn_cdc_get_max_lsn();
SELECT __$operation, OrderID, Status, Amount
FROM cdc.fn_cdc_get_all_changes_dbo_Orders(@from, @to, N'all');| __$operation | Change |
|---|---|
| 1 | delete |
| 2 | insert |
| 3 | update, the row before |
| 4 | update, the row after |
With N'all', an update comes back once, as operation 4; N'all update old' returns the before row as well.
How does a load use it?
- 1Last positionthe log position the last run reached
- 2Read changesfrom that position to the newest
- 3Applydelete, insert or update by key
- 4Save positiononly when the apply succeeded
It is the watermark pattern again, with a log position instead of a date. Save the position only after the target has the changes, and a failed run picks them up next time.
What goes wrong?
- Changes expire. A cleanup job removes change rows after a retention period (three days by default). A load that stops for longer misses changes and needs a full reload.
- The table's columns change. A new column is not captured until a new capture instance is made for the table.
- SQL Server Agent is off. On SQL Server, capture and cleanup run as Agent jobs. Azure SQL Database runs them with its own scheduler.
Where can you check this?
- Microsoft Learn: What is change data capture (CDC)?
- Microsoft Learn: Enable and disable change data capture
- Microsoft Learn: cdc.fn_cdc_get_all_changes
Where do you start?
Load only the rows that changed, with a watermark is the same load with a date, and says when that is enough. The open course Upserts into Azure SQL builds the merge by key the changes are applied with.