Synopsis:
I’m using Change Tracking to track changes in a Child table. I’d like to group them by a Parent key, and keep track of one MAX value. Works great, except for deletes.
Details:
Two tables,
Categories:
CategoryID, int (PK)
Name, varchar()
StoreItems:
StoreItemID, int (PK)
CategoryID, int (FK)
Name, varchar()
The number of StoreItems per Category is rather small, and I want clients to just grab the ENTIRE list any time it detects ANY change to StoreItems assigned to a Category (insert, update or delete).
I have Change Tracking turned on for StoreItems.
The following query works great:
SELECTS.CategoryID,MAX(c.SYS_CHANGE_VERSION)asMaxChangeNum
FROMStoreItemsASS
CROSSAPPLYCHANGETABLE
(VERSIONStoreItems,(StoreItemID),(S.StoreItemID))ASc
WHERES.CategoryID= 1
GROUPBYS.CategoryID
This yields:
CategoryID MaxChangeNum
1 72
This gives me the highest Change number for all StoreItems changed by Category, which each client could keep track of, but the problem is, it doesn’t include deleted records.
When I use CHANGETABLE(CHANGES…) , I can see the deleted records, but I no longer have the Category they were in. All it keeps is StoreItemID.
I tried a DELETE trigger, where I still have access to the CategoryID, but unfortunately the CHANGETABLE updates have not occurred yet.
Any suggestions on how to include deletes when tracking StoreItems changes BY Category?