Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
SQL Puzzle
Message
De
18/02/2002 19:32:52
 
 
À
18/02/2002 18:19:45
Bob Thomsen
Fabtrol Systems, Inc
Eugene, Oregon, États-Unis
Information générale
Forum:
Visual FoxPro
Catégorie:
Client/serveur
Titre:
Divers
Thread ID:
00621714
Message ID:
00621724
Vues:
9
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
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform