Alex
Alex

Reputation: 131

How to get incremental change from SQL Server 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

Upvotes: 0

Views: 1745

Answers (1)

Simon Hughes
Simon Hughes

Reputation: 3574

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 ;

Upvotes: 2

Related Questions