I just need to delete repeating user info from database. My c# code is below, but I just wonder how to achieve this in SQL rather without using cursor. I think the trick starts with the fetching the first rows or the remaining rows of the whole repeating datasets separated by email.
In C#, I gather repeating emails by groups of 1000 and delete the remaining rows after skipping the first one.
List<string> top1000_emails;
do
{
top1000_emails = sql.dbCommand.GetFirstColumn<string>(@"select top 1000 email
from UserBase
group by email
having COUNT(email) > 1");
for (int i = 0; i < top1000_emails.Count; i++)
{
var tmpids = sql.dbCommand.GetFirstColumn<long>("select [Id] from UserBase where email = {0}", top1000_emails[i]).Skip(1);
sql.dbCommand.DeleteByIds<UserBase>(tmpids);
}
} while (top1000_emails.Count > 0);
You can do ti simply through SQL, like this (if you have SQL Server 2005 or higher):