Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 29th, 2005, 02:52 PM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default Dealing with duplicate rows/column values

I have a table in which data in a column is "supposed" to be unique. The data for this table has been brought in from another database vendor which did not do a very good job enforcing uniqueness. Before I apply a unique constraint on the appropriate column, I need to check for uniqueness and clean up the data.

Sure enough, I found a number of instances where data is duplicated. For example, in the results below, the CustNum column value is supposed to be unique, but as you can see, there are duplicates.

ID CustNum Name Status

1 29226 Bob A
14 29226 Bobby A

4 2207 Larry A
27 2207 Larry A

9 4704 Jim I
56 4704 Jimmy A
99 4704 Jimbo A

What I would like to do is, for each duplicate pair, take one of the records and change the custnum value to another number (defined by me). If this were just a onetime deal, i would do it somewhat manually, however this will not be a onetime deal.

I am trying to find an "easy" way to do this. I have a query already that returns the duplicate information. Now i would like to loop through this data and change one of the numbers for each duplicate pair.

I have thought of one way to do it but it is not very elegant. Any information would be greatly appreciated. Hopefully I explained this ok.

Thanks...

Scott

__________________
========================
Scott Klein
Author of:
Professional SQL Server 2005 XML
Professional WCF Programming: .NET Development with the Windows Communication Foundation
Professional LINQ
========================
 
Old September 29th, 2005, 08:53 PM
Friend of Wrox
 
Join Date: Aug 2004
Posts: 385
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sounds like a perfect job for a cursor. Where you save the previous value into a variale, itterate to the next row, then if the previous value matches the current value, you change it as you described. Are you familiar with cursors? This is basically one of their most useful areas.

 
Old September 29th, 2005, 09:04 PM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Yes, I'm very familiar with cursors, and that is the way I started to solve this problem, before stopping to see if there was a more efficient method. But, I think I agree with you. It looks like a cursor is the only way.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting duplicate values in primary key column, Shuchik SQL Server ASP 1 August 31st, 2007 05:38 AM
duplicate rows msrnivas Classic ASP Components 1 August 26th, 2004 02:07 AM
Detecting duplicate rows. Daniel Walker PHP Databases 6 June 21st, 2004 02:39 AM
Checking Duplicate values and delete rows ppenn Excel VBA 2 February 3rd, 2004 06:57 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.