• I agree that you need to write something to go with your script.

    And I believe that your script is slightly flawed, it looks like it will only delete the first duplicate record of each duplicate set and will leave all the rest intact (your test data only had 2 duplicates in any set, as far as I noticed, so it worked fine for that data set). I would suggest making the following minor change, which would keep the first record of each duplicate set and delete all others (the first one in theory anyway, SQL gives no guarantee on order of duplicate records, and how would you know anyway? 😉 ).

    DELETE FROM cteDV WHERE RID <> 1

    You could also saved a keystroke and said '> 1', but both will do the same thing in this case.

    For a quick demo using asterisk in the select statement of your CTE works but I would suggest spelling out the columns so that the code doesn't break should the source table structure change.

    Over all a nice little script for getting rid of annoying duplicate records, thank you for sharing it with us.