--Clean up MSP_TASK_CUSTOM_FIELD_VALUES_SHADOW
IF object_id('tempdb..#TempTaskCFShadow') IS NOT NULL
BEGIN
drop table #TempTaskCFShadow
END
CREATE table #TempTaskCFShadow (pid uniqueidentifier, tid uniqueidentifier, revCount int, mdpid uniqueidentifier)
INSERT #TempTaskCFShadow (pid, tid, revCount, mdpid)
SELECT PROJ_UID, TASK_UID, MAX(DELETED_REV_COUNTER), MD_PROP_UID
FROM MSP_TASK_CUSTOM_FIELD_VALUES_SHADOW
GROUP BY PROJ_UID , TASK_UID, MD_PROP_UID
DELETE
FROM MSP_TASK_CUSTOM_FIELD_VALUES_SHADOW
FROM MSP_TASK_CUSTOM_FIELD_VALUES_SHADOW as originalTable, #TempTaskCFShadow AS newtable
where originalTable.MD_PROP_UID = newtable.mdpid AND
originalTable.PROJ_UID = newtable.pid AND
originalTable.TASK_UID = newtable.tid AND
originalTable.DELETED_REV_COUNTER <> newtable.revCount
DROP TABLE #TempTaskCFShadow
--Clean up MSP_ASSN_CUSTOM_FIELD_VALUES_SHADOW
IF object_id('tempdb..#TempAssnCFShadow') IS NOT NULL
BEGIN
drop table #TempAssnCFShadow
END
CREATE table #TempAssnCFShadow (pid uniqueidentifier, asid uniqueidentifier, revCount int, mdpid uniqueidentifier)
INSERT #TempAssnCFShadow (pid, asid, revCount, mdpid)
SELECT PROJ_UID, assn_uid, MAX(DELETED_REV_COUNTER), MD_PROP_UID
FROM MSP_ASSN_CUSTOM_FIELD_VALUES_SHADOW
GROUP BY PROJ_UID , ASSN_UID, MD_PROP_UID
DELETE
FROM MSP_ASSN_CUSTOM_FIELD_VALUES_SHADOW
FROM MSP_ASSN_CUSTOM_FIELD_VALUES_SHADOW as originalTable, #TempAssnCFShadow AS newtable
where originalTable.MD_PROP_UID = newtable.mdpid AND
originalTable.PROJ_UID = newtable.pid AND
originalTable.ASSN_UID= newtable.asid AND
originalTable.DELETED_REV_COUNTER <> newtable.revCount
DROP TABLE #TempAssnCFShadow
--Clean up MSP_PROJ_RES_CUSTOM_FIELD_VALUES_SHADOW
IF object_id('tempdb..#TempProjResCFShadow') IS NOT NULL
BEGIN
drop table #TempProjResCFShadow
END
CREATE table #TempProjResCFShadow (pid uniqueidentifier, resid uniqueidentifier, revCount int, mdpid uniqueidentifier)
INSERT #TempProjResCFShadow (pid, resid, revCount, mdpid)
SELECT PROJ_UID, RES_UID, MAX(DELETED_REV_COUNTER), MD_PROP_UID
FROM MSP_PROJ_RES_CUSTOM_FIELD_VALUES_SHADOW
GROUP BY PROJ_UID , RES_UID, MD_PROP_UID
DELETE
FROM MSP_PROJ_RES_CUSTOM_FIELD_VALUES_SHADOW
FROM MSP_PROJ_RES_CUSTOM_FIELD_VALUES_SHADOW as originalTable, #TempProjResCFShadow AS newtable
where originalTable.MD_PROP_UID = newtable.mdpid AND
originalTable.PROJ_UID = newtable.pid AND
originalTable.RES_UID = newtable.resid AND
originalTable.DELETED_REV_COUNTER <> newtable.revCount
DROP TABLE #TempProjResCFShadow
--Clean up MSP_PROJ_CUSTOM_FIELD_VALUES_SHADOW
IF object_id('tempdb..#TempProjCFShadow') IS NOT NULL
BEGIN
drop table #TempProjCFShadow
END
CREATE table #TempProjCFShadow (pid uniqueidentifier, revCount int, mdpid uniqueidentifier)
INSERT #TempProjCFShadow (pid, revCount, mdpid)
SELECT PROJ_UID, MAX(DELETED_REV_COUNTER), MD_PROP_UID
FROM MSP_PROJ_CUSTOM_FIELD_VALUES_SHADOW
GROUP BY PROJ_UID , MD_PROP_UID
DELETE
FROM MSP_PROJ_CUSTOM_FIELD_VALUES_SHADOW
FROM MSP_PROJ_CUSTOM_FIELD_VALUES_SHADOW as originalTable, #TempProjCFShadow AS newtable
where originalTable.MD_PROP_UID = newtable.mdpid AND
originalTable.PROJ_UID = newtable.pid AND
originalTable.DELETED_REV_COUNTER <> newtable.revCount
DROP TABLE #TempProjCFShadow