?
Solved

how to update one record when there are duplicates?

Posted on 1998-08-20
7
Medium Priority
?
175 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 4

Expert Comment

by:mcix
ID: 1089674
0
 
LVL 1

Accepted Solution

by:
DennisH earned 200 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

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

741 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