The table in this case was a user info table called UserInfo, appropriately. I am using asp.net Membership, as well as a new table analogous to the UserInfo table, but with improvements (atomic storage of data, constraints, etc.) so I needed to create an entry in membership using the old table's email and password, and then create a corresponding entry in my user table using pretty much everything from the old table.
First, I needed to remove all of the superfluous duplicates, as email was to become a unique identifier in my new table (and act as the username in my Membership tables). These guys helped me out. They look like some good blokes. Anyway, I just wanted to delete the top 1 record in all the instances where there were 2 or more records containing the same email. My script looked like this:
DELETE TOP (1) FROM USERS
WHERE EMAIL IN
(
SELECT EMAIL FROM USERINFO
GROUP BY EMAIL
HAVING COUNT(EMAIL) > 1
)
The good thing about this script is also its flaw. It will only delete one record at a time where the email exists more than once, ensuring that you don't delete both of the records (you want to keep one!). Fortunately, there were only about 120 duplicates, and my simple and farily quick and effective solution was to just press "f5" 120 times. Done! I didn't want to mess with cursors when I can just monkey-push the button like I'm playing Track-n-Field at the local nickel arcade.
How would you have done this more elegantly?
Next, how to move data from one table to another table the E-Z way (or not).
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment