Solved

how to update one record when there are duplicates?

Posted on 1998-08-20
7
170 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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…
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 date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

895 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

15 Experts available now in Live!

Get 1:1 Help Now