将列增量更新1

时间:2018-03-19 07:45:34

标签: sql sql-server

我有一个由数字1,2,3,5,6,9 ......组成的列。

请告知SQL存储过程如何从1开始更新此列,依此类推where ORDERBY date_column(date)

1 个答案:

答案 0 :(得分:0)

你可以使用这样的东西。请按方向检查订单,如果2行包含相同的日期值,该怎么办(您可以在ORDER BY内的OVER子句中添加其他表达式。

CREATE PROCEDURE dbo.ResetNumber
AS
BEGIN

    ;WITH Ranking AS
    (
        SELECT
            T.ColumnWhichHoldsTheValue,
            Ranking = ROW_NUMBER() OVER (ORDER BY T.date_column ASC) -- Please see if it should be ASC or DESC
        FROM
            YourTable AS T
    )
    UPDATE T SET
        ColumnWhichHoldsTheValue = T.Ranking
    FROM
        Ranking AS T

END