Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SQL Puzzle
Message
From
18/02/2002 19:32:52
 
 
To
18/02/2002 18:19:45
Bob Thomsen
Fabtrol Systems, Inc
Eugene, Oregon, United States
General information
Forum:
Visual FoxPro
Category:
Client/server
Title:
Miscellaneous
Thread ID:
00621714
Message ID:
00621724
Views:
26
Not in one...

Now, you didn't specify what database product that you're using. This will work for SQL Server 2000.
DECLARE @newOrderKey TABLE (
 newKey int IDENTITY(1,1),
 oldKey int 
)

INSERT INTO @newOrderKey (oldKey)
SELECT OrderKey FROM sometable ORDER BY OrderKey ASC

UPDATE sometable
SET OrderKey = (SELECT newKey FROM @newOrderKey nk WHERE nk.oldKey = sometable.OrderKey)
OK, you can do it in one query but the performance will be horrible. It requires counting the distinct OldKey values prior the the current OldKey value:
UPDATE sometable
SET OrderKey = (SELECT COUNT(DISTINCT OrderKey)+1 FROM sometable st2 WHERE st2.OrderKey < sometable.OrderKey)
Be wary of this one. It's really going to depend on how sophisticated the query processor is. SQL Server should be able to handle it.

-MIke
Michael Levy
MCSD, MCDBA
ma_levy@hotmail.com
Previous
Reply
Map
View

Click here to load this message in the networking platform