USE MYDB GO CREATE PROCEDURE MergePurgeMetadata @numdays int = 5 AS DECLARE @purgedate datetime SELECT @purgedate = DATEADD (dd, -1 * @ numdays + 1, getdate()) DELETE MSmerge_contents FROM MSmerge_ contents a,MSmerge_genhistory b WHERE a.generation = b.generation AND b.coldate < CONVERT(char(10), @purgedate,101) AND a.generation > 0 DELETE MSmerge_genhistory WHERE coldate < CONVERT(char(10),@purgedate,101) AND pubid IS NOT NULL