Handle Duplicate Records

  • Comments posted to this topic are about the item Handle Duplicate Records

  • I've found the easy way to remove duplicates is with the UNION statement:

    select * into aTempTable

    from SourceTable

    UNION

    select * from SourceTable

    GO

    truncate table SourceTable

    insert into SourceTable

    select * from aTempTable

    GO

    Does this work for you?

  • Sorry I am still a newbie when it comes to programming like this...

    What is the "@ID ..." signify. I figured out the ListOfFields but just can't figure out what the "@ID..." signifies.

    Thank You

    Ted

  • No problem. Anything with an @ in front is a variable. @ID would hold a value for each row of the table that somehow uniquely identifies that row. So, the cursor has you looping through all the duplicate values. Each time it gets to "Fetch Next Into @ID", it puts the next value into the variable.

    Then, the first time through the loop, it assigns that to @KeepID. And any subsequent loops, you would run all your logic to merge / move / delete the duplicate record.

  • So the "..." after the "@ID" is not needed?

    I do understand the basics of variables, but am still learning how to use them properly.

    Thanks Ted

  • No. Sorry, the example is just pseudo-code, you'll have to replace some stuff to make it actually work.

    I put the '...' there in case you wanted to add more variables than just @ID ("FETCH NEXT INTO @ID, @Whatever, @Something). You could use the other variable in whatever processing you need to run, or if the primary key on that table is multiple columns you'd need to grab all of them just to delete.

Viewing 6 posts - 1 through 5 (of 5 total)

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