IF @C_TABLE_NAME IN ('AX_BATCH_HISTORY') --RETAINED BASED ON FIELDS IN THE DATABASES_2_COLLECT TABLE
BEGIN
DELETE QS FROM AX_BATCH_HISTORY QS WITH (ROWLOCK, READPAST)
INNER JOIN DATABASES_2_COLLECT DC ON QS.SERVER_NAME = DC.LINKED_SERVER AND DC.DATABASE_NAME = QS.DATABASE_NAME
WHERE QS.SERVER_NAME = @SERVER_NAME AND QS.DATE < DATEADD(MONTH,DC.RETAIN_HISTORY_MONTHS * -1, GETDATE()) AND QS.FLAG = 'M'
SET @ROW_COUNT = @ROW_COUNT + @@ROWCOUNT
--REH DELETE DAYS TOTALS FROM QUERY_HISTORY
DELETE QS FROM AX_BATCH_HISTORY QS WITH (ROWLOCK, READPAST)
INNER JOIN DATABASES_2_COLLECT DC ON QS.SERVER_NAME = DC.LINKED_SERVER AND DC.DATABASE_NAME = QS.DATABASE_NAME
WHERE QS.SERVER_NAME = @SERVER_NAME AND QS.DATE < DATEADD(MONTH,DC.RETAIN_HISTORY_MONTHS * -1, GETDATE()) AND QS.FLAG = 'D'
SET @ROW_COUNT = @ROW_COUNT + @@ROWCOUNT
END
ELSE -- !!!! THIS ELSE statement is missing in original script ,causing some PURGE statements to fail
IF @C_TABLE_NAME IN ('INDEX_HISTORY', 'QUERY_HISTORY') --RETAINED BASED ON FIELDS IN THE DATABASES_2_COLLECT TABLE
IF @C_TABLE_NAME IN ('AX_BATCH_HISTORY') --RETAINED BASED ON FIELDS IN THE DATABASES_2_COLLECT TABLE
ELSE -- !!!! THIS ELSE statement is missing in original script ,causing some PURGE statements to fail
IF @C_TABLE_NAME IN ('INDEX_HISTORY', 'QUERY_HISTORY') --RETAINED BASED ON FIELDS IN THE DATABASES_2_COLLECT TABLE