in

Dot Net Mafia

Group site for developer blogs dealing with (usually) .NET, SharePoint 2013, SharePoint 2010, Office 365, SharePoint Online, and other Microsoft products, as well as some discussion of general programming related concepts.

This Blog

Syndication

Archives

Corey Roth [MVP]

A SharePoint MVP bringing you the latest time saving tips for SharePoint 2013, SharePoint 2010, Office 365, SharePoint Online, MOSS 2007, ASP.NET, LINQ, and Visual Studio 2012.

Finding a duplicate key when importing data

This isn't really .NET 2.0 related or anything, but I still think it might be useful. I don't know how many times I have been charged with a task of importing data from a text file or from an access database or whatever and I ran into an issue where the supposed primary keys have duplicate rows in them. Well, with the aid of google, I found a relatively easy way to find the duplicates using the HAVING SQL clause.

The query below, will spit out the values committing the offense against the primary key. You may need to import your data to a temp table in SQL first without a key set. It will also work directly in Access.

SELECT COUNT(*), PrimaryKeyColumn
FROM TableName
GROUP BY PrimaryKeyColumn
HAVING Count(*) > 1

That's all there is to it. If you have a compound primary key, you can find it in a similar way.

SELECT COUNT(*), PrimaryKeyColumn1, PriamryKeyColumn2
FROM TableName
GROUP BY PrimaryKeyColumn1, PrimaryKeyColumn2
HAVING Count(*) > 1

That's not .NET related, but I hope it helps you out someday.

Read the complete post at http://www.dotnettipoftheday.com/blog.aspx?id=237

2012 dotnetmafia.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems