Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 How to update the top of an ordered list?

Author  Topic 

Cornelius19
Starting Member

30 Posts

Posted - 2008-11-11 : 18:45:04
Hi,

I have a table with two columns: prod and sel. The prod column contains numeric values and is not null. I would like to update the sel column of the 1000 records with the highest prod value. I tried the following but got an "incorrect syntax" error message:

UPDATE MyTable
SET TOP 1000 sel = 1
ORDER BY prod DESC

Do you have any suggestions how to do this?

Cornelius

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-11-11 : 19:01:35
UPDATE MyTable
SET sel = 1
FROM (SELECT TOP 1000 sel FROM MyTable ORDER BY prod DESC) t

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

cvraghu
Posting Yak Master

187 Posts

Posted - 2008-11-12 : 00:37:16
quote:
Originally posted by tkizer

UPDATE MyTable
SET sel = 1
FROM (SELECT TOP 1000 sel FROM MyTable ORDER BY prod DESC) t



Shouldn't we have where clause in this query to connect the primary keys. Just curious. I don't have SQL server to test it out.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-12 : 00:41:55
[code]UPDATE MyTable
SET sel = (SELECT MAX(prod) FROM MyTable)
WHERE prod IN
(SELECT TOP 1000 prod FROM MyTable ORDER BY prod)[/code]
Go to Top of Page

Cornelius19
Starting Member

30 Posts

Posted - 2008-11-12 : 12:34:00
Great! Thanks a lot!

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-12 : 12:39:59
Welcome
Go to Top of Page
   

- Advertisement -