Last active
September 18, 2024 17:07
-
-
Save petervandivier/cd2736f35873cd6d4096651970977aae to your computer and use it in GitHub Desktop.
🦆 rubber ducking __$update_mask & __$operation for SQL Server CDC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| https://learn.microsoft.com/en-us/sql/relational-databases/system-tables/cdc-capture-instance-ct-transact-sql | |
| wanted to check that for ins/del ops (2 & 1 respectively) that the full update bitmask always applies | |
| it does: | |
| - 0x0f bitmask on 4 cols | |
| - 0x7f bitmask on 7 cols | |
| - 0xFF bitmask on 8 cols | |
| */ | |
| go | |
| /* | |
| use CdcTest | |
| go | |
| exec sys.sp_cdc_disable_db | |
| go | |
| */ | |
| use [master] | |
| go | |
| drop database if exists CdcTest; | |
| go | |
| create database CdcTest; | |
| go | |
| use CdcTest | |
| go | |
| exec sp_changedbowner 'sa' | |
| go | |
| exec sys.sp_cdc_enable_db; | |
| go | |
| create table dbo.foo ( | |
| id int not null primary key, | |
| a char(1), | |
| b char(1), | |
| c char(1), | |
| d char(1), | |
| e char(1), | |
| f char(1), | |
| g char(1) | |
| ); | |
| go | |
| insert dbo.foo | |
| (id,a,b,c) | |
| values | |
| (1,'a','b','c'), | |
| (2,'e','f','g'), | |
| (3,'h','i','j'); | |
| go | |
| select * | |
| from dbo.foo; | |
| go | |
| -- https://learn.microsoft.com/en-us/sql/relational-databases/track-changes/enable-and-disable-change-data-capture-sql-server | |
| -- https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sys-sp-cdc-enable-table-transact-sql | |
| exec sp_cdc_enable_table | |
| @source_schema = N'dbo', | |
| @source_name = N'foo', | |
| @role_name = null, | |
| @supports_net_changes = 1; | |
| go | |
| /* | |
| Update mask evaluation will be disabled in net_changes_function because the CLR configuration option is disabled. | |
| SQLServerAgent is not currently running so it cannot be notified of this action. | |
| SQLServerAgent is not currently running so it cannot be notified of this action. | |
| SQLServerAgent is not currently running so it cannot be notified of this action. | |
| SQLServerAgent is not currently running so it cannot be notified of this action. | |
| */ | |
| go | |
| -- start SQL Agent, enable CLR, & restart demo | |
| exec sp_configure 'clr enabled', 1; | |
| reconfigure; | |
| go | |
| /* -- success from sp_cdc_enable_table | |
| Job 'cdc.CdcTest_capture' started successfully. | |
| Job 'cdc.CdcTest_cleanup' started successfully. | |
| */ | |
| go | |
| select * | |
| from cdc.dbo_foo_CT | |
| -- empty | |
| go | |
| update dbo.foo | |
| set a = a | |
| where id = 1; | |
| go | |
| select * | |
| from cdc.dbo_foo_CT | |
| -- empty | |
| go | |
| update dbo.foo | |
| set a = 'a' | |
| where id = 1; | |
| go | |
| select * | |
| from cdc.dbo_foo_CT | |
| -- empty | |
| go | |
| update dbo.foo | |
| set a = 'x' | |
| where id = 1; | |
| go | |
| select * | |
| from cdc.dbo_foo_CT | |
| -- $ops 3 & 4 , valid update bitmask | |
| go | |
| insert dbo.foo | |
| (id,a,b,c) | |
| values | |
| (4,'x','y','z') | |
| go | |
| select * | |
| from cdc.dbo_foo_CT | |
| -- $op 2 | |
| -- 0x0f bitmask on 4 cols | |
| -- 0x7f bitmask on 7 cols | |
| -- 0xFF bitmask on 8 cols | |
| go | |
| delete | |
| from dbo.foo | |
| where id = 4; | |
| go | |
| select * | |
| from cdc.dbo_foo_CT | |
| -- $op 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment