SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Handle Duplicate Records


Handle Duplicate Records

Author
Message
erik-507089
erik-507089
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 33
Comments posted to this topic are about the item Handle Duplicate Records
jnichols-797753
jnichols-797753
SSC Journeyman
SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)

Group: General Forum Members
Points: 89 Visits: 158
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
tlocke-512364
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 Visits: 46
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
erik-507089
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 33
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
tlocke-512364
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 Visits: 46
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
erik-507089
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 33
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search