gpt4 book ai didi

sql-server - 如何 : Interpret the MSSQL CDC "__$update_mask" field

转载 作者:行者123 更新时间:2023-12-05 06:46:33 29 4
gpt4 key购买 nike

由于我在使用“$update_mask”字段时遇到了很大的困难(出于性能原因我不想调用 fn_cdc_is_bit_set),这里是 C# 代码:

  • updateMask 是来自 CDC“fn_cdc_get_all_changes_[table]”查询的“__$update_mask”字段的内容
  • colOrdinal 是要从中获取已更改标志的列的序号(使用“n_cdc_get_column_ordinal”检索此值)

结果是“hasChanged”标志。如果该字段在更新中已更改,则设置为 true。

请注意,这适用于 SQL Servers 2008 和 2012,但可能不适用于 future 的版本。

byte[] updateMask = this.UpdateMask;
unchecked
{
byte relevantByte = updateMask[(updateMask.Length - 1) - ((colOrdinal - 1) / 8)];
int bitMask = 1 << ((colOrdinal - 1) % 8);
var hasChanged = (relevantByte & bitMask) != 0;

return hasChanged;
}

最佳答案

只需使用 BitArray获取更改的列的序号索引。反转掩码并将其添加到 BitArray ,所有设置为 true 的数组值都是相应索引处的更改列。

byte[] updateMask = this.UpdateMask;  
Array.Reverse(updateMask);
BitArray columnBits = new BitArray(bitmask);

// To match your code sample, you can just check if the column bit is set at the ordinal.
bool hasChanged = columnBits[colOrdinal];

// You could loop and get the column indexes like this as well.
for (int i = 0; i < columnBits.Length; i++)
{
// If the bit is set to true then the column at the same index was modified.
if (columnBits[i])
{
// Do something with modified column at index 'i'.
}
}

我已尽力使代码示例尽可能简单易读。记得先检查数组索引是否存在等等。

关于sql-server - 如何 : Interpret the MSSQL CDC "__$update_mask" field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14607325/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com