• I agree you do not really need the temporary table unless you want it to be able to review results before making final changes to the "live" database.

    If you want to forego the temp table, below is a variation (without loops or temp tables) for removing duplicates if you have some other criteria to determine which duplicate to keep. If you have to pick off a completely identical record, then I guess you would have to do some sort of @@Rowcount function as shown in a previous posting.

    If you do have criteria to pick out which record(s) to remove then the removal is a simple Delete statement. Again if the records are exact duplicates then it is a coin-toss to find which one to take out so you would need a rowcount, cursor kind of method, index of columns with dup key (as in the original article and some posts), or a select distinct into a temporary table following that method.

    The Delete Statement (for duplicate phone values with differing pdate values)

    DELETE p1

    FROM @phonenum p1

    INNER JOIN @phonenum p2

    ON p1.phone = p2.phone and p1.pdate > p2.pdate

    The Delete statement in test code

    Declare @phonenum table (phone varchar(10), pdate int)

    insert -- set up for test

    into @phonenum (phone,pdate)

    select '1',1 union all

    select '2',2 union all

    select '3',3 union all

    select '3',2 union all

    select '4',2 union all

    select '5',1 union all

    select '5',7 union all

    select '5',3 union all

    select '6',1

    select 'original table', phone, pdate -- show before picture

    from @phonenum

    /* Remove the dups from the original table based on oldest date */

    DELETE p1

    FROM @phonenum p1

    INNER JOIN @phonenum p2

    ON p1.phone = p2.phone and p1.pdate > p2.pdate

    select 'after delete', phone, pdate -- show after picture

    from @phonenum

    Toni