SQL Server 2008. Is it possible to create Change Data Capture (or Change Tracking) for several related tabled in a db? For example many-to-many relation.
How it will look like?
Never used it but from the MSDN doc I would say : yes!
prepare the db first:
ALTER DATABASE [yourdb] SET ALLOW_SNAPSHOT_ISOLATION ON
ALTER DATABASE [yourdb]
SET CHANGE_TRACKING = ON
(CHANGE_RETENTION = 2 DAYS, AUTO_CLEANUP = ON)
Now enable change tracking on the required table(s):
ALTER TABLE [yourdb].[yourschema].[yourtable]
ENABLE CHANGE_TRACKING
Query for changes:
IF @sync_initialized = 0
SELECT *
FROM [yourschema].[yourtable] LEFT OUTER JOIN
CHANGETABLE(CHANGES [yourschema].[yourtable], @sync_last_received_anchor) CT
ON CT.[yourkey] = [yourschema].[yourtable].[yourkey]
ELSE
BEGIN
SELECT *
FROM Sales.Customer
JOIN CHANGETABLE(CHANGES [yourschema].[yourtable], @sync_last_received_anchor) CT
ON CT.[yourkey] = [yourschema].[yourtable].[yourkey]
WHERE (CT.SYS_CHANGE_OPERATION = 'I'
AND CT.SYS_CHANGE_CREATION_VERSION
<= @sync_new_received_anchor)
END