Deleting Duplicate batches of rows

  • George Damien Varkey

    SSC-Addicted

    Points: 440

    Comments posted to this topic are about the item Deleting Duplicate batches of rows

  • totlani.ritesh

    SSC Rookie

    Points: 32

    This code does not give proper results. I tried to work on this but its really untested bad work by the developer.

  • George Damien Varkey

    SSC-Addicted

    Points: 440

    Ritesh what is it that is not working, can you let me know.

  • Nick Hobbs

    SSC Enthusiast

    Points: 177

    I think this is a much more efficient example for deleting duplicate rows using a single query and not having to resort to using Cursors:

    http://dipakjpatel.blogspot.com/2007/08/delete-duplicate-rows-using-single.html

    Regards

    Nick

  • tzara

    Say Hey Kid

    Points: 663

    don't need a temp table. don't need cursors.

    you can remove dups in one statement.

    cheers.

  • fhanlon

    SSChampion

    Points: 13127

    The code worked fine for me. There is more than one way to do this. So if you don't like this way try another but in terms of results this code works.



    Francis

  • Nick Hobbs

    SSC Enthusiast

    Points: 177

    I am sure the code works fine, however the article implied that this is one situation where you have to use cursors - which are very inefficient if you can do it alternatively in a single query:

    >>This code works in SQL Server 2005 and above, because it uses the delete top (N) statement. Although using a cursor is not always a good idea but there are situations where we have to use it.

    I am sure the cursor method with copying the data to a temporary table is satisfactory for small sets of data. I was surprised however to see it listed as a featured article in SQL Server Central's daily newsletter. When having to regularly run a query against e.g. 1m+ records that may involve several tables, it helps to know if there are more efficient methods. I thought it was worth pointing to a more efficient method that I came across recently as this is not a situation where we have to use cursors.

    Regards

    Nick

  • irina-813672

    SSC Rookie

    Points: 37

    A much simpler way to remove duplicates would be to use SELECT DISTINCT * from tableName

  • Nick Hobbs

    SSC Enthusiast

    Points: 177

    SELECT DISTINCT * from tableName

    While this gives you a list of records without the duplicates, it does not help much in deleting the duplicates. Additional code is still need to do something to remove the duplicates from the original table which this article addresses along with the other suggestions.

  • blairxy

    Valued Member

    Points: 69

    Thank you for sharing this. It was my introduction to cursors, and I was able to adapt it to keep my input table intact (by adding top(1) to another table, instead of deleting top @cnt).

    Going forward, it does appear the common table expression approach linked by Nick has more promise.

    All I wanted was something like 'Ignore row errors on insert'!

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply