Solved

Updating using current row number

Posted on 2004-08-10
4
1,956 Views
Last Modified: 2008-03-17
I'm trying to update a field for a series of records to contain sequential numbers, like a fix to remove gaps in the numbers.

So that, if I can return the following table:

ID  |  Value
87  |    5
89  |    11
95  |    18
98  |    23

I want to be able to do a single UPDATE query, to make:

ID  |  Value
87  |    1
89  |    2
95  |    3
98  |    4

I would prefer an answer that is the same in both MSSQL and MySQL, but an answer for either one of them will earn the points
0
Comment
Question by:neur0maniak
[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
4 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 200 total points
ID: 11761439
In MSSQL Server, I would perform this operation like this:

CREATE TABLE TEMP ( ID INT )
INSERT INTO TABLE TEMP SELECT ID FROM yourtable
CREATE CLUSTERED INDEX CIDX_ID ON TABLE TEMP ( ID )
ALTER TABLE TEMP ADD VALUE INT IDENTITY (1,1)
UPDATE yourtable SET VALUE = TEMP.VALUE
FROM yourtable JOIN TEMP on TEMP.ID = yourtable.ID
DROP TABLE TEMP

I don't know about how I would do this in MySQL, but I assume it won't work that way...

Actually, if you use a cursor, this can be done quite easily (pseudo-code):
open the cursor (SELECT ID, VALUE FROM yourtable ORDER BY ID ASC )
@value = 0
for each row
   @Value = @Value+1
   update current row SET VALUE = @Value


That's it. Of course, not a single UPDATE. You might think about an update like the current:
UPDATE yourtable SET Value = (select count(*) from Yourtable ti WHERE ti.ID <= yourtable.ID )

It can work in MSSQL (not sure in MySQL), but performance is really bad...

Cheers
0

Featured Post

Webinar: Security & Encryption in the MySQL world

Join Percona’s Solutions Engineer, Dimitri Vanoverbeke as he presents “Security and Encryption in the MySQL world” on Thursday, July 6, 2017 at 7:00 am PDT / 10:00 am EDT (UTC-7).

Question has a verified solution.

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

Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

690 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