Solved

how to update one record when there are duplicates?

Posted on 1998-08-20
7
169 Views
Last Modified: 2010-03-19
Is there a way to update only one record when there are duplicate records in the table?  
The reason there are duplicates is because the distinct key fields haven't been populated yet, but I need to perform an update on only one particular record before I can fill in the distinct key fields.  Any suggestions?
0
Comment
Question by:Lorik
7 Comments
 
LVL 4

Expert Comment

by:mcix
ID: 1089671
An easy way would be to have an identity field in your table.  On the insert, you could return the @@Identity variable to your code from a stored proc that does the insert.  With that value, you would have a unique criteria for the where clause for the update.

If you can't modify the table struct, there are possibly other ways.
0
 
LVL 10

Expert Comment

by:bret
ID: 1089672
I know this would work in Sybase SQL Server, not sure in MS SQL, but give it a try (on a test table, please!).
-- limit action to a single row
set rowcount 1
go
update mytable
where dupkeyvalue = <x>
go
-- return to affecting full set of rows
set rowcount 0
go

0
 

Author Comment

by:Lorik
ID: 1089673
MS-SQL doesn't have ROWCOUNT funtion, I wish it did.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Expert Comment

by:mcix
ID: 1089674
0
 
LVL 1

Accepted Solution

by:
DennisH earned 100 total points
ID: 1089675
SQL Server does not allow selected updates unless there is a primary key designated for a table.
0
 
LVL 5

Expert Comment

by:mayhew
ID: 1089676
Lorik,

I'm not sure I understand exactly what you're trying to do.  If I understand your question, you might want to do an UPDATE...WHERE, something like:

UPDATE table SET <whatever>
WHERE <afield> = (select distinct <afield> from table)

This may give you a starting point, again depending on what exactly you want to do.

Don
0
 
LVL 5

Expert Comment

by:mayhew
ID: 1089677
Lorik,

Sorry, I wasn't thinking straight.  What I wrote before will not work.  Please ignore it.

If you want to update only one row, you have to find something unique about it.  It doesn't have to be a key, but it has to be unique.  

Otherwise you have to do something procedural like using a cursor to find the set, fetch the first record and then update it.  Let us know if you need help with this.  :)

Don
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now