tags:

views:

26

answers:

1

I'm trying to get up to speed on CDC by using it to create a very simple replication process. Let's say I have CDC set up for tables 'A' and 'B', which happen to have a FK relationship where 'A' is the parent and 'B' is the child. I want to replicate changes to these two tables to another DB.

I'm thinking of creating an SSIS package (or something) that will read the CDC tables associated with 'A' and 'B' to move the changes over, on some regular basis. The problem I'm trying to find a solution for is this. It seems to me that, because there is a FK relationship between 'A' and 'B', I have to know in what order to process the changes. For example, if rows are being inserted in both tables, I probably have to insert rows into 'A' first, before 'B'. Or, if rows are being deleted from both tables, I probably have to delete rows from 'B' first and then 'A'.

So, my question is this. Since this can get enormously complex as I add more tables, how do I get around this issue?

A: 

That should be anon issue: you simply apply the changes in the order they where applied on the primary. Since the changes passed the validation criteria on the primary, they should pass it on the replica as well. cdc.fn_cdc_get_all_changes returns the change sequence number is _$seqval, derived from the change LSN. You simply follow this sequence number and apply the operations one by one.

Remus Rusanu
@Remus - If I understand you correctly, you are suggesting I query the cdc.lsn_time_mapping table to get all the relevent LSN's. Then, for each one, I query all the CDC tables holding changed data, looking for each LSN, and then apply the changes from the table containing each LSN. This would work, but I could potentially be querying hundreds of tables until I find the one holding the LSN I'm looking for, for each change I have. I don't think that would be particularly efficient, and a maintenance headache.
Randy Minder
No, you don't query the tables directly at all. You use the CDC procedures and functions, as described in http://msdn.microsoft.com/en-us/library/cc645858.aspx
Remus Rusanu
@Remus - Yes I realize I query the tables via the UDFs. But I still have to query each of the associated table UDFs until I find the LSN I'm looking for, correct?
Randy Minder
I think you're right. I got carried away by the Transactional Replication engine that powers CDC and did not consider that CDC exposes tables individually.
Remus Rusanu