如何从 SQL Server CDC 获取增量更改

发布于 2024-10-27 10:27:29 字数 232 浏览 1 评论 0原文

例如,如何从 SQL Server CDC 获取增量更改:

我在数据库上有表 customer,任何插入、更新或删除都将保存在 CDC 表 customer 上。

我想要一张表来告诉我下一步应该做什么改变。

例如,每 10 分钟我只会读取最后 10 分钟的更改并保存此时间范围,因此下次我会得到最后一次加上 10 分钟。 (增量读取)

任何人都有 CDC 的好例子

谢谢

How can I get incremental change from SQL Server CDC for instance:

I have table customer on the database and any insert, update or delete will be saved on the CDC table customer.

I would like to have one table that would tell me what change should I get next.

For example every 10 minutes I would read only the last 10 minutes changes and save this time range, so next time I would get the last time plus 10 minutes. ( incremental reads)

Any one has any good example with CDC

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昔日梦未散 2024-11-03 10:27:29

您需要将最后处理的 LSN 保存在数据库中的某个位置。

CREATE TABLE cdc_consumer
   (cdc_consumer_id SMALLINT NOT NULL CONSTRAINT PK_cdc_consumer PRIMARY KEY CLUSTERED
  , [description] VARCHAR(200) NOT NULL
  , capture_instance SYSNAME NOT NULL
  , last_start_lsn BINARY(10) NULL
  , last_seqval BINARY(10) NULL
  , date_last_consumed DATETIME NULL
  , CONSTRAINT UQ_cdc_consumer UNIQUE NONCLUSTERED ([description], capture_instance));

当您处理完一批 CDC 记录后,请更新此表。

要获取起点和终点,请使用以下命令:

CREATE PROCEDURE cdc_consumer_start_batch
      @cdcConsumerId SMALLINT
    , @startLsn BINARY(10) OUTPUT
    , @seqval BINARY(10) OUTPUT
    , @endLsn BINARY(10) OUTPUT
AS 
BEGIN
    DECLARE @lastLsn BINARY(10) ;
    DECLARE @captureInstance SYSNAME ;

    SELECT  @lastLsn         = last_start_lsn,
            @seqval          = last_seqval,
            @captureInstance = capture_instance
    FROM    cdc_consumer
    WHERE   cdc_consumer_id = @cdcConsumerId ;

    IF (@captureInstance IS NULL) 
    BEGIN
        DECLARE @errorText VARCHAR(50) ;

        SET @errorText = 'Cannot find a cdc consumer with id: "' + CAST(@cdcConsumerId AS VARCHAR(10)) + '"';
        RAISERROR(@errorText,16,1) ;
    END

    SET @startLsn = sys.fn_cdc_get_min_lsn(@captureInstance) ;
    IF (@lastLsn > @startLsn) 
        SET @startLsn = @lastLsn;

    SET @endLsn = sys.fn_cdc_get_max_lsn() ;

    IF (@endLsn < @startLsn)
        RETURN 1 ;
    ELSE 
        RETURN 0 ;
END ;

You need to save the last LSN you processed in your database somewhere.

CREATE TABLE cdc_consumer
   (cdc_consumer_id SMALLINT NOT NULL CONSTRAINT PK_cdc_consumer PRIMARY KEY CLUSTERED
  , [description] VARCHAR(200) NOT NULL
  , capture_instance SYSNAME NOT NULL
  , last_start_lsn BINARY(10) NULL
  , last_seqval BINARY(10) NULL
  , date_last_consumed DATETIME NULL
  , CONSTRAINT UQ_cdc_consumer UNIQUE NONCLUSTERED ([description], capture_instance));

Update this table when you've finished processed a batch of CDC records.

To get the start and end points use this:

CREATE PROCEDURE cdc_consumer_start_batch
      @cdcConsumerId SMALLINT
    , @startLsn BINARY(10) OUTPUT
    , @seqval BINARY(10) OUTPUT
    , @endLsn BINARY(10) OUTPUT
AS 
BEGIN
    DECLARE @lastLsn BINARY(10) ;
    DECLARE @captureInstance SYSNAME ;

    SELECT  @lastLsn         = last_start_lsn,
            @seqval          = last_seqval,
            @captureInstance = capture_instance
    FROM    cdc_consumer
    WHERE   cdc_consumer_id = @cdcConsumerId ;

    IF (@captureInstance IS NULL) 
    BEGIN
        DECLARE @errorText VARCHAR(50) ;

        SET @errorText = 'Cannot find a cdc consumer with id: "' + CAST(@cdcConsumerId AS VARCHAR(10)) + '"';
        RAISERROR(@errorText,16,1) ;
    END

    SET @startLsn = sys.fn_cdc_get_min_lsn(@captureInstance) ;
    IF (@lastLsn > @startLsn) 
        SET @startLsn = @lastLsn;

    SET @endLsn = sys.fn_cdc_get_max_lsn() ;

    IF (@endLsn < @startLsn)
        RETURN 1 ;
    ELSE 
        RETURN 0 ;
END ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文