Hi,
We use stored procedure like below to get the data on intial load as well as subsequent changes. The underllined part of the stored procedure to get the last_sync_version is slightly different from what Microsoft has suggested. (not using CHANGE_TRACKING_CURRENT_VERSION). That is because we download data to staging from multiple sources and from their we want to process data from one source at a time (DATA_SOURCE_KEY filter).
What we notice is that, when there is no data for a table for sometime, once the change tracking gets purged for that table, we keep hitting the condition @LastVersionId <@MinVersionId. The MIN_VALID_VERSION keeps changing even though there is no change in that table. Looks like the MIN_VALID_VERSION is a global number and not specific to a table. Is there a problem with the way we are getting the @Next_Version. Can we use the CHANGE_TRACKING_CURRENT_VERSION even though we want to download subset of data from the table (only records matching for one data_source_key at a time). Appreciate your answers.
CREATE PROCEDURE [dbo].[PDW_GetSiteDVAudit](@DataSourceKey AS INT,@LastVersionID AS BIGINT, @NextVersionID AS BIGINT OUTPUT)AS
BEGIN TRY
BEGIN TRANSACTION
BEGIN
DECLARE @MinVersionID bigint = 0, @max_version bigint = 0;
SELECT @NextVersionID = 0;
SET @MinVersionID = CHANGE_TRACKING_MIN_VALID_VERSION(OBJECT_ID('dbo.SiteDVAudit'));
IF (@LastVersionID < @MinVersionID)
BEGIN
SELECT @NextVersionID = isnull(MAX(SYS_CHANGE_VERSION),@MinVersionID) from CHANGETABLE(CHANGES SiteDVAudit , NULL) CT where DataSource_Key = @DataSourceKey
SELECT
s.Id,
Site_Id ,
SiteDesc,
Deleted,
Local_Id,
s.DataSource_Key,
Data_Task_Id
FROM
dbo.SiteDVAudit s
WHERE
s.DataSource_Key = @DataSourceKey
END
ELSE
BEGIN
SELECT @NextVersionID = isnull(MAX(SYS_CHANGE_VERSION),@LastVersionID) from CHANGETABLE(CHANGES SiteDVAudit , @LastVersionID) CT where DataSource_Key = @DataSourceKey
SELECT
s.Id,
Site_Id ,
SiteDesc,
Deleted,
Local_Id,
s.DataSource_Key,
Data_Task_Id
FROM
dbo.SiteDVAudit s
RIGHT OUTER JOIN
CHANGETABLE(CHANGES SiteDVAudit, @LastVersionID) CT
ON s.Id = CT.Id AND
s.DataSource_Key = CT.DataSource_Key
WHERE
s.DataSource_Key = @DataSourceKey
END
END
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
RETURN -1
END CATCH