Set up Change Tracking or Change Data Capture yourself¶
The wizard sets up Change Tracking or Change Data Capture on the source when its login has the rights for it. Use these steps when your security policy doesn't allow that, or to turn Change Data Capture off when you no longer need it.
Change Tracking without a privileged login¶
When your security policy doesn't allow the login the rights in
Give the login its rights for Change Tracking, an administrator does the
privileged part first, and the agent's login gets only the rights it needs to run. In the source database (replace
mydb, myschema, mytab and SQDR with your names):
-
Create the control schema, the agent's user (for an existing login
SQDR), and its rights to create tables and procedures in the schema:USE mydb; CREATE SCHEMA SQDR; CREATE USER SQDR FOR LOGIN SQDR WITH DEFAULT_SCHEMA = SQDR; ALTER AUTHORIZATION ON SCHEMA::SQDR TO SQDR; GRANT CREATE TABLE TO SQDR; GRANT CREATE PROCEDURE TO SQDR; -
Turn on snapshot isolation and Change Tracking for the database. The agent reads the changes in a snapshot transaction, so that SQL Server's cleanup of old change information and new changes don't affect a read in progress. The retention is how long SQL Server keeps the change information: if the agent is stopped for longer, the tables need a new baseline. The wizard uses two days; choose more if the agent may be stopped for longer.
ALTER DATABASE mydb SET ALLOW_SNAPSHOT_ISOLATION ON; ALTER DATABASE mydb SET CHANGE_TRACKING = ON (CHANGE_RETENTION = 2 DAYS, AUTO_CLEANUP = ON);To check them:
SELECT * FROM sys.change_tracking_databases WHERE database_id = DB_ID('mydb'); SELECT snapshot_isolation_state FROM sys.databases WHERE database_id = DB_ID('mydb'); -
For each table to replicate (it needs a primary key), grant
SELECTandVIEW CHANGE TRACKING, and turn on Change Tracking:GRANT SELECT ON myschema.mytab TO SQDR; GRANT VIEW CHANGE TRACKING ON myschema.mytab TO SQDR; ALTER TABLE myschema.mytab ENABLE CHANGE_TRACKING WITH (TRACK_COLUMNS_UPDATED = ON);To generate these statements for every table in a schema, run this and then run its output:
SELECT 'GRANT SELECT ON ' + QUOTENAME(SCHEMA_NAME(schema_id)) + '.' + QUOTENAME(name) + ' TO SQDR; ' + 'GRANT VIEW CHANGE TRACKING ON ' + QUOTENAME(SCHEMA_NAME(schema_id)) + '.' + QUOTENAME(name) + ' TO SQDR; ' + 'ALTER TABLE ' + QUOTENAME(SCHEMA_NAME(schema_id)) + '.' + QUOTENAME(name) + ' ENABLE CHANGE_TRACKING WITH (TRACK_COLUMNS_UPDATED = ON);' FROM sys.tables WHERE SCHEMA_NAME(schema_id) = 'myschema';
When you then create the agent with this login, the wizard finds the schema, snapshot isolation and Change Tracking in place and leaves them as they are.
Turn on Change Data Capture yourself¶
The wizard turns on CDC for the database, and the agent for each table when you subscribe to it. To do it yourself, use the same calls:
USE mydb;
EXEC sys.sp_cdc_enable_db;
EXEC sys.sp_cdc_enable_table @source_schema = N'myschema', @source_name = N'mytab',
@role_name = NULL, @supports_net_changes = 0;
To see whether CDC is on for the database and which tables have it:
SELECT name, is_cdc_enabled FROM sys.databases WHERE name = 'mydb';
EXEC sys.sp_cdc_help_change_data_capture;
SELECT name, is_tracked_by_cdc FROM sys.tables WHERE is_tracked_by_cdc = 1;
Besides the tables you subscribe to, the list includes the agent's own SQ_BASELINES control table, which the agent
subscribes to itself. In SQL Server Management Studio, a table's Properties › General page shows
Table is replicated as True for a table with CDC on.
Turn off Change Data Capture¶
Deleting a subscription or a Capture Agent doesn't turn off Change Data Capture on the source: the agent can't tell whether another application uses it. When you no longer need it, for example after you switch the agent to Change Tracking or stop replicating some tables, turn it off yourself. Make sure no other application uses CDC on those tables or that database.
For some tables only:
- In the Data Replicator Manager, delete their subscriptions.
- Find each table's capture instance with
EXEC sys.sp_cdc_help_change_data_capture;. -
Turn off CDC for each table:
EXEC sys.sp_cdc_disable_table @source_schema = N'dbo', @source_name = N'mytab', @capture_instance = N'dbo_mytab';
For the whole database, when you no longer replicate from it:
- In the Data Replicator Manager, delete the subscriptions.
- In the SQDR Control Center, delete the agent.
- Turn off CDC for the database, which also turns it off for all its tables:
EXEC sys.sp_cdc_disable_db;.
To keep only a few of many CDC tables, you can clear CDC for every table at once and let the agent turn it on again for the tables you still subscribe to. The source tables must not change while you do this:
- In the Data Replicator Manager, delete the subscriptions you no longer need.
- In the SQDR Control Center, stop the agent.
-
Turn CDC off and on again for the database:
USE mydb; EXEC sys.sp_cdc_disable_db; EXEC sys.sp_cdc_enable_db; -
Set the agent's startup mode to Warm (restored) (see Startup mode), and start it. It turns CDC on again for the subscribed tables.
- Unless you're sure the source tables didn't change while CDC was off, run new baselines for the subscriptions.
If CDC was turned off by mistake, see Troubleshooting › SQL Server.