I'm syncing data from SQL server A towards SQL server B. Sometimes data gets removed in the source, to prevent that I have to sync the full table everytime I would like to track those deleted records.
I don't want to work with triggers since it is our ERP database and I know that the ERP software supplier doesn't like to see this.
That's why I'm checking if I could work with Change Tracking instead. In my test case I've enabled Change Tracking on my test database and enabled it on my test table. It does track Inserts (I) and Updates (U) in the SYS_CHANGE_OPERATION column but it doens't track the Deletes (D).
Is there a setting that I missed and still has to be set? Or am I missing something?
I've tried to remove the record in different ways just to see if they do get removed then. But unfortunately I didn't get the desired result.
You can use a workaround to indirectly track deletions using Change Tracking. You can leverage the fact that Change Tracking does track updates and inserts. You can identify the deleted records by comparing the changed version of the last synchronization with the current change version.
Here's a high-level approach you can follow:
Enable Change Tracking on the table: Make sure Change Tracking is enabled on the database and table you want to track.
Perform initial synchronization: Perform a full synchronization of the table to ensure that all the existing data is captured by Change Tracking.
Track changes during subsequent synchronizations: During each subsequent synchronization, retrieve the latest change version from the target table.
Identify deleted records: Compare the last change version from the previous synchronization with the current change version. Any records that were present in the last synchronization but not in the current synchronization are considered deleted.
Handle deleted records: Process the deleted records as per your requirements. This may involve marking them as deleted in the destination table or performing any necessary actions.
By comparing the change versions, you can effectively identify the deleted records even though they are not explicitly tracked by Change Tracking.
It's worth noting that this approach relies on the assumption that deletions are accompanied by an update or insert operation. If a record is deleted without any accompanying changes, it may not be captured using this method.
YourDatabase is the name of your database, SourceTable is the table you want to synchronize from, TargetTable is the table you want to synchronize to, and SyncStateTable is a table that keeps track of the last change version.
Note that you need to create the SyncStateTable and populate it with the last change version during each synchronization. The table can have a single row with a column like LastChangeVersion to store the last change version.
You may need to modify the code to suit your specific table schema and requirements.