使用CDC或T-SQL与CDC跟踪字段的更改

时间:2017-08-10 16:05:13

标签: tsql sql-server-2014 unpivot cdc

为更新操作设置CDC结果集。仅更新了area字段。 enter image description here

在之前的sreenshot中,只包含表中的部分gield。领域更多。他们不时更新一些不更新。在以下查询中,我尝试按可用视图中的字段显示更改统计信息。

with History AS (
SELECT
cz.GUID as Id, 
cz.category,
isnull(cz.area, 0) as area, 
isnull(cz.oilwidthmin,0) as oilwidthmin, 
isnull(cz.oilwidthmax,0) as oilwidthmax, 
isnull(cz.efectivwidthmin,0) as efectivwidthmin,
isnull(cz.efectivwidthmax,0) as efectivwidthmax,
isnull(cz.koafporistmin,0) as koafporistmin, 
isnull(cz.koafporistmax,0) as koafporistmax,
CASE cz.__$operation 
WHEN 1 THEN 'DELETE'
WHEN 2 THEN 'INSERT'
WHEN 3 THEN 'Before UPDATE'
WHEN 4 THEN 'After UPDATE'
END operation,
map.tran_begin_time as beginT, 
map.tran_end_time as endT
FROM cdc.fn_cdc_get_all_changes_dbo_EXT_GeolObject_KategZalezh(sys.fn_cdc_get_min_lsn('dbo_EXT_GeolObject_KategZalezh'), sys.fn_cdc_get_max_lsn(), 'all') AS cz 
INNER JOIN  [cdc].[lsn_time_mapping] map
    ON cz.[__$start_lsn] = map.start_lsn
)
SELECT  field, val, operation, beginT, endT FROM History
unpivot ( [val] for field in
(
--category,
area, 
oilwidthmin, 
oilwidthmax, 
efectivwidthmin, 
efectivwidthmax, 
koafporistmin, 
koafporistmax))t    where id = '2D166098-7CBD-4622-9EB0-000070506FE6'   

查询结果如下: enter image description here

但之前的结果包含额外数据。 预期结果必须如下: enter image description here

我知道CDC会按行跟踪更改。或者也许我错了?如果不是,我如何在SQL中对val字段进行一些比较。我对t-sql知之甚少,而且我想到的一切都被游标以某种方式使用。有任何想法吗? 也许以某种方式使用CT(变更跟踪)?也许以某种方式使用group by

几乎正确答案。 Folowing查询返回预期结果:

WITH History AS (
    SELECT
        *,
        CASE cz.__$operation 
            WHEN 1 THEN 'DELETE'
            WHEN 2 THEN 'INSERT'
            WHEN 3 THEN 'Before UPDATE'
            WHEN 4 THEN 'After UPDATE'
            END operation,
        map.tran_begin_time as beginT, 
        map.tran_end_time as endT
    FROM cdc.fn_cdc_get_all_changes_dbo_EXT_GeolObject_KategZalezh(sys.fn_cdc_get_min_lsn('dbo_EXT_GeolObject_KategZalezh'), sys.fn_cdc_get_max_lsn(), 'all') AS cz 
        INNER JOIN  [cdc].[lsn_time_mapping] map
            ON cz.[__$start_lsn] = map.start_lsn
    where cz.GUID = '2D166098-7CBD-4622-9EB0-000070506FE6'
),
UnpivotedValues AS(
    SELECT  guid, field, val, operation, beginT, endT 
    FROM History
        UNPIVOT ( [val] FOR field IN
        (
            area, 
            oilwidthmin, 
            oilwidthmax, 
            efectivwidthmin, 
            efectivwidthmax, 
            koafporistmin, 
            koafporistmax
        ))t
),
UnpivotedWithLastValue AS (
    SELECT 
        *,
        --Use LAG() to get the last value for the same field
        LAG(val, 1) OVER (PARTITION BY field ORDER BY BeginT) LastVal
    FROM UnpivotedValues
)
--Filter out record where the value equals the last value for the same field
SELECT * FROM UnpivotedWithLastValue WHERE val <> LastVal OR LastVal IS NULL ORDER BY guid

此查询的结果如下所示: enter image description here

但是当WHERE cz.GUID =不存在或者在WHERE谓词中使用多个GUID的查询时,我得到了以下结果:

enter image description here 此结果为两个GUID。第一行LastVal的值必须为16691.与第4行的val类似。

2 个答案:

答案 0 :(得分:1)

您无法将CDC设置为仅跟踪已更改的列的值。但是,您可以非常轻松地过滤掉查询中未更改的值。

考虑以下查询,它是原始查询的简化副本:

WITH History AS (
    SELECT
        *,
        CASE cz.__$operation 
            WHEN 1 THEN 'DELETE'
            WHEN 2 THEN 'INSERT'
            WHEN 3 THEN 'Before UPDATE'
            WHEN 4 THEN 'After UPDATE'
            END operation,
        map.tran_begin_time as beginT, 
        map.tran_end_time as endT
    FROM cdc.fn_cdc_get_all_changes_Dbo_YourTable(sys.fn_cdc_get_min_lsn('Dbo_YourTable'), sys.fn_cdc_get_max_lsn(), 'all') AS cz 
        INNER JOIN  [cdc].[lsn_time_mapping] map
            ON cz.[__$start_lsn] = map.start_lsn
),
UnpivotedValues AS(
    SELECT id, field, val, operation, beginT, endT, t.tran_id
    FROM History
        UNPIVOT ( [val] FOR field IN
        (Column1, Column2, Column3))t
),
UnpivotedWithLastValue AS (
    SELECT 
        *,
        --Use LAG() to get the last value for the same field
        LAG(val, 1) OVER (PARTITION BY id, field ORDER BY BeginT) LastVal
    FROM UnpivotedValues
)
--Filter out record where the value equals the last value for the same field
SELECT * FROM UnpivotedWithLastValue WHERE val <> LastVal OR LastVal IS NULL
ORDER BY Id, beginT

在这个查询中,我使用了LAG()函数来获取每个字段的最后一个值。根据此值,您可以过滤掉最终查询中未更改的记录,如上所示。

答案 1 :(得分:0)

在您的情况下,您可以使用ROW_NUMBER函数按顺序对更改进行编号 - 在此之后,您可以将每个顺序更改与前一个相关联(基于字段和ID),并仅输出具有diffent值的行。 / p>

这样的事情:

WITH 
History AS 
(
SELECT
    cz.GUID as Id, 
    cz.category,
    isnull(cz.area, 0) as area, 
    isnull(cz.oilwidthmin,0) as oilwidthmin, 
    isnull(cz.oilwidthmax,0) as oilwidthmax, 
    isnull(cz.efectivwidthmin,0) as efectivwidthmin,
    isnull(cz.efectivwidthmax,0) as efectivwidthmax,
    isnull(cz.koafporistmin,0) as koafporistmin, 
    isnull(cz.koafporistmax,0) as koafporistmax,
    CASE 
        cz.__$operation 
        WHEN 1 THEN 'DELETE'
        WHEN 2 THEN 'INSERT'
        WHEN 3 THEN 'Before UPDATE'
        WHEN 4 THEN 'After UPDATE'
    END operation,
    map.tran_begin_time as beginT, 
    map.tran_end_time as endT,
    ROW_NUMBER() OVER (PARTITION BY cz.GUID ORDER BY map.tran_end_time ASC) as rn
FROM 
    cdc.fn_cdc_get_all_changes_dbo_EXT_GeolObject_KategZalezh(sys.fn_cdc_get_min_lsn('dbo_EXT_GeolObject_KategZalezh'), sys.fn_cdc_get_max_lsn(), 'all') AS cz 
INNER JOIN  
    [cdc].[lsn_time_mapping] map ON cz.[__$start_lsn] = map.start_lsn
),

History2 AS
(
    SELECT  id, field, val, operation, beginT, endT, rn FROM History
    unpivot ( [val] for field in
    (
    --category,
    area, 
    oilwidthmin, 
    oilwidthmax, 
    efectivwidthmin, 
    efectivwidthmax, 
    koafporistmin, 
    koafporistmax))t    
    where id = '2D166098-7CBD-4622-9EB0-000070506FE6'
)

-- return the values that were inserted first
SELECT
    a.*
FROM
    History2 a
WHERE 
    a.rn=1

UNION ALL

-- ... and then return only the values that are different from the previous ones
SELECT
    a.*
FROM
    History2 a
INNER JOIN
    History2 b ON a.id = b.id AND a.field=b.field AND a.rn = b.rn-1 AND a.value<>b.value
WHERE
    a.rn>1

顺便说一句;您还可以将CDC配置为仅跟踪某些列中的更改,而不是整个表中的更改。查看sys.sp_cdc_enable_table存储过程的@captured_column_list。