Skip to content

Replicate new rows without a Capture Agent

Incremental replication needs a Capture Agent for the source database. For a source that no Capture Agent supports (for example Db2 for z/OS, or another database reached through ODBC; see Databases), a scheduled snapshot subscription can still copy only the rows added since its last run, so that the destination stays close to the source without copying the whole table each time.

Use it only for tables whose rows are inserted, never updated or deleted, such as logs, journals or transaction histories: changes to rows already copied are not replicated. When the source is supported by a Capture Agent, use incremental replication instead.

How it works

  • The source table has a column whose value grows with every inserted row and is never reused: in SQL Server a rowversion column, elsewhere an identity column, a sequence number or an insert timestamp.
  • A small tracking table holds, per subscription, two values of that column: CURMAX, the highest value when the current run started, and LASTMAX, the highest value already copied.
  • The subscription runs on a schedule and appends to the destination table. Its processing keeps the tracking table up to date, and its criteria selects the rows between the two values:
    1. Source processing before the run sets CURMAX to the column's current highest value.
    2. The criteria copies the rows above LASTMAX and up to CURMAX. Rows inserted while the run copies are left for the next run.
    3. Processing after the run sets LASTMAX to CURMAX.

The tracking table can be in the source database (the simplest setup, below) or in the destination database (see Keep the tracking table at the destination).

Set it up

The example uses a SQL Server source database SALESDB and a table dbo.STABLE. Run the SQL in SQL Server Management Studio (or your database's SQL tool, adapted to its syntax).

  1. Add the row-version column to the source table, if it has no suitable column, and index it:

    ALTER TABLE dbo.STABLE ADD TSCOL rowversion NOT NULL;
    CREATE INDEX IX_TSCOL ON dbo.STABLE (TSCOL);
    

    A rowversion (formerly timestamp) value isn't a date or time: SQL Server generates a new, unique, increasing binary value for each inserted row, whatever the system clock does. Don't use a datetime column, which can repeat or go backwards.

  2. Create the tracking table in the source database, and a row for the subscription, named like the subscription you'll create:

    CREATE TABLE dbo.SUBTRACK (
      SUBNAME varchar(128) NOT NULL PRIMARY KEY,
      CURMAX  binary(8) DEFAULT 0,
      LASTMAX binary(8) DEFAULT 0);
    
    INSERT INTO dbo.SUBTRACK (SUBNAME) VALUES ('STABLE');
    
  3. Create the snapshot subscription for the table (see Create subscriptions), named STABLE like the tracking row, with:

    • Destination DDL Options: Create if does not exist, else use existing, so that saving the subscription later never drops the rows already copied (see Destination DDL Options);
    • Baseline Replication Options: Append replicated rows to existing data (see Baseline Replication Options).
  4. On the Processing tab, enter the source processing:

    • before the run:

      UPDATE SALESDB.dbo.SUBTRACK SET CURMAX = (SELECT MAX(TSCOL) FROM SALESDB.dbo.STABLE)
        WHERE SUBNAME = 'STABLE'
      
    • after the run:

      UPDATE SALESDB.dbo.SUBTRACK SET LASTMAX = CURMAX WHERE SUBNAME = 'STABLE'
      
  5. On the Criteria tab, enter:

    WHERE (STABLE.TSCOL > (SELECT LASTMAX FROM SALESDB.dbo.SUBTRACK WHERE SUBNAME = 'STABLE'))
      AND (STABLE.TSCOL <= (SELECT CURMAX FROM SALESDB.dbo.SUBTRACK WHERE SUBNAME = 'STABLE'))
    

    Verify Criteria finds no rows until the first run has set CURMAX: that's expected.

  6. On the Schedule tab, run the subscription at a regular interval (see Scheduling). Leave enough time between runs for a run and its processing to finish: a run that is still busy when the next one is due is skipped.

  7. Save, and run the subscription once with Run. The first run copies every row (LASTMAX starts at 0); later runs copy only the rows added since.

To check the progress, compare LASTMAX in the tracking table with the highest value in the source table.

More tables

For another table in the same database, add its row-version column and index (step 1), add a row to the same tracking table for its subscription (the INSERT in step 2), and create its subscription (steps 3 to 7) with its own names. For a table in another database, create a tracking table there too.

Remove copied rows from the source

To keep the source table small, delete the rows already copied, at any time, without affecting the destination:

DELETE FROM SALESDB.dbo.STABLE
  WHERE TSCOL <= (SELECT LASTMAX FROM SALESDB.dbo.SUBTRACK WHERE SUBNAME = 'STABLE');

Keep the tracking table at the destination

The tracking table can also be in the destination database. The final update of LASTMAX is then made at the destination, with the replicated rows, so that the run succeeds or fails as a whole. The source reads and updates the tracking table through a linked server: a SQL Server object that reaches another database through an ODBC data source. (Other databases have a similar feature, for example the Oracle Database Gateway for ODBC.)

The example uses a Db2 for i destination, with the tracking table in the destination schema MYSCHEMA.

  1. Add the row-version column to the source table, as in step 1.
  2. Create the tracking table at the destination, with a unique index so that the linked server can update it, and the subscription's row:

    CREATE TABLE MYSCHEMA.SUBTRACK (
      SUBNAME VARCHAR(128) NOT NULL,
      CURMAX  CHAR(8) FOR BIT DATA WITH DEFAULT,
      LASTMAX CHAR(8) FOR BIT DATA WITH DEFAULT,
      PRIMARY KEY (SUBNAME));
    CREATE UNIQUE INDEX MYSCHEMA.SUBTRACKIX ON MYSCHEMA.SUBTRACK (SUBNAME);
    INSERT INTO MYSCHEMA.SUBTRACK (SUBNAME) VALUES ('STABLE');
    

    The tracking table and the destination table must be journaled (on Db2 for i, created in a schema, which journals its tables) so that their changes are made under commitment control. When SQDR creates the destination table, choose a destination schema whose tables are journaled.

  3. Create a linked server on the source SQL Server (the one that holds the source table, which may not be the SQDR server), through a 64-bit ODBC System DSN for the destination database on that computer:

    1. In SQL Server Management Studio, under Server Objects, right-click Linked Servers and choose New Linked Server.
    2. Enter a Linked server name without spaces or special characters, for example DESTDB, and choose Other data source.
    3. As Provider, choose Microsoft OLE DB Provider for ODBC Drivers (MSDASQL); as Data source, enter the DSN name exactly as the ODBC Data Source Administrator shows it.
    4. On the Security page, choose Be made using this security context and enter a destination user and password that can update the tracking table.

    The ODBC driver must pass binary (FOR BIT DATA) values unconverted: for a Db2 for i destination, check that the data source doesn't translate binary data (CCSID 65535) to text.

  4. Turn off Use single transaction for source processing, so that the source processing is kept even if the replication fails: in Replication defaults for every subscription, or in the Group Replication Settings of a snapshot group that runs the subscription.

  5. Create the snapshot subscription as in step 3, with Append replicated rows to existing data, and enter its processing:

    • source processing before the run (DESTDB the linked server, MYDB the destination's catalog, which for Db2 for i is the database name):

      UPDATE DESTDB.MYDB.MYSCHEMA.SUBTRACK
        SET CURMAX = (SELECT CONVERT(binary(8), MAX(TSCOL)) FROM SALESDB.dbo.STABLE)
        WHERE SUBNAME = 'STABLE'
      
    • destination processing after the run:

      UPDATE MYSCHEMA.SUBTRACK SET LASTMAX = CURMAX WHERE SUBNAME = 'STABLE'
      
  6. Enter the Criteria:

    WHERE (TSCOL > CONVERT(binary(8),
             (SELECT LASTMAX FROM DESTDB.MYDB.MYSCHEMA.SUBTRACK WHERE SUBNAME = 'STABLE')))
      AND (TSCOL <= CONVERT(binary(8),
             (SELECT CURMAX FROM DESTDB.MYDB.MYSCHEMA.SUBTRACK WHERE SUBNAME = 'STABLE')))
    
  7. Schedule, save and run it as in steps 6 and 7 of Set it up.

For more tables, repeat steps 1, the INSERT of step 2, and 5 to 7. To remove copied rows from the source, read LASTMAX through the linked server:

DELETE FROM SALESDB.dbo.STABLE
  WHERE TSCOL <= (SELECT LASTMAX FROM DESTDB.MYDB.MYSCHEMA.SUBTRACK WHERE SUBNAME = 'STABLE');