Handle Duplicate Records

  • erik-507089

    Valued Member

    Points: 51

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

  • jnichols-797753

    Ten Centuries

    Points: 1085

    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?

  • tlocke-512364

    Valued Member

    Points: 50

    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

  • erik-507089

    Valued Member

    Points: 51

    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.

  • tlocke-512364

    Valued Member

    Points: 50

    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

  • erik-507089

    Valued Member

    Points: 51

    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 6 (of 6 total)

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