Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Updating using current row number

Posted on 2004-08-10
4
Medium Priority
?
1,962 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 800 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

610 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