【问题标题】:mssql cdc update_mask filter changes made only in column TSmssql cdc update_mask 过滤器更改仅在列 TS
【发布时间】:2020-07-16 07:52:20
【问题描述】:

我想在我的 mssql cdc 表中查找仅更改了“TS”列的行。

所以我找到了一些逻辑来检查特定列是否已更改(这可行),但我需要检查是否只有列 TS 已更改:

SET @colorder = sys.fn_cdc_get_column_ordinal('dbo_mytable', 'TS')

SELECT case when substring([__$update_mask],len([__$update_mask]) - ((@colorder-1)/8),1) & power(2,(@colorder-1)%8) > 0 then 1 else 0 end

FROM cdc.fn_cdc_get_all_changes_dbo_MYTABLE(@from_lsn, @to_lsn, 'all') PD

【问题讨论】:

  • 您使用的是哪种 DBMS 产品? “SQL”只是所有关系数据库都使用的一种查询语言,而不是特定数据库产品的名称。请为您使用的数据库产品添加tagWhy should I tag my DBMS
  • 问题更多是关于位掩码逻辑
  • 这使得使用的 DBMS 更加重要

标签: sql sql-server sql-server-2016 bitmask cdc


【解决方案1】:

我一直想写这样的函数,谢谢你给了我一个真正去做的理由。

请自己做一些单元测试,我只做了一些非常基本的检查

-- inline tabular function because it's more versatile
-- cross applies used purely for ease of reading, 
-- could just make nested calls but hard to read. Up to you.
-- pass null to flip, otherwise pass explicit value you want the bit to be set to
create function dbo.setbit(@b varbinary(128), @bitpos tinyint, @value bit = null) 
returns table as
return
(
    select      result = cast(result.val as varbinary(128))
    from        (select len(@b) - ((@bitpos - 1) / 8)) bytepos(val)
    cross apply (select substring(@b, bytepos.val, 1)) byte(val)
    cross apply (select power(2, (@bitpos - 1) % 8)) mask(val)
    cross apply (
                   select  cast
                           (
                               case @value
                                   when 1 then byte.val | mask.val
                                   when 0 then byte.val & ~mask.val
                                   else byte.val ^ mask.val
                               end
                               as binary(1)
                           )
                ) newbyte(val)
    cross apply (select stuff(@b, bytepos.val, 1, newbyte.val)) result(val)
);

-- scalar wrapper for tabular function
create function dbo.setbitscalar(@b varbinary(128), @bitpos tinyint, @value bit = null) 
returns varbinary(128) as
begin
    return (select result from dbo.setbit(@b, @bitpos, @value));
end

-- how it works
declare @b varbinary(128) = 0x0101 -- 2 bytes!
select 
    dbo.setbitscalar(@b, 1, 1),        -- set bit 1 to 1
    dbo.setbitscalar(@b, 1, 0),        -- set bit 1 to 0
    dbo.setbitscalar(@b, 1, default)   -- flip bit 1
    
-- how to use it in your case:
-- set the @colorder bit in the mask to zero, 
-- then see if the resulting mask is zero
-- if it is, then only TS changed
SET @colorder = sys.fn_cdc_get_column_ordinal('dbo_mytable', 'TS')

select      only_TS_changed = iif(t.result = 0x, 1, 0) 
from        cdc.fn_cdc_get_all_changes_dbo_MYTABLE(@from_lsn, @to_lsn, 'all') PD
cross apply dbo.setbit(PD.[__$update_mask], @colorder, 0) t

【讨论】:

  • 很好的解决我的问题,非常感谢@allmhuran,我只需要改变一件事:only_TS_changed = iif(t.result = 0x00, 1, 0),而不是与整数比较,我每次都必须与十六进制原因 = 0dez 结果比较为真
  • 非常好。如果与文字 0 比较,它将把长二进制截断为前 4 个字节并提升为 int,因此它只检查前 4 个字节是否为空。固定。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-12
相关资源
最近更新 更多