简体   繁体   中英

How to fill `[Order]` column by order number in SQL Server?

How to fill [Order] column by order number in SQL Server 2000?

For example, I have a SQL:

select Id, Tilte 
from Tbl 
order by Date

I need to write order number from this query to column [Order] of Tbl table.

How to do this?

Thanks a lot for the help!

You can use ROW_NUMBER :

WITH CTE AS
(
   SELECT Id, Title, [Order] 
   , OrderNumber = ROW_NUMBER() OVER (ORDER BY Date)
   FROM Tbl 
)
UPDATE CTE SET [Order] = CTE.OrderNumber;

Here's a fiddle: http://sqlfiddle.com/#!3/8831d/2/0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM