按订单更新查询

时间:2019-07-18 00:44:16

标签: sql sql-update sql-order-by

我正在尝试根据另一列的简短内容更新一列。我正在使用order by,但是在使用select top(10000)时,我的增量数要达到4000系列,但我需要从101开始,但最初我是declare int 100

DECLARE @IncrementValue int
SET @IncrementValue = 100

UPDATE CabecDoc 
SET CDU_NumberBook = @IncrementValue,
    @IncrementValue = @IncrementValue + 1 
FROM
    (SELECT TOP(7000) *
     FROM CabecDoc
     WHERE data BETWEEN '2019-05-01' AND '2019-07-17'
       AND Entidade = 'VD4' 
       AND tipodoc = 'VD' AND CDU_SimbolBook = '*'
     ORDER BY NumDoc ASC) CabecDoc 

我需要将列从101更新为通过7000条记录增加的数字。

1 个答案:

答案 0 :(得分:1)

这就是您需要的。无需声明变量。

UPDATE
  CabecDoc
SET
  CDU_NumberBook = 100 + RowNum
FROM
  (
    Select
      top(7000) *,  
      ROW_NUMBER() OVER(
        ORDER BY
          NumDoc
      ) AS RowNum
    FROM
      CabecDoc
    WHERE
      data between '2019-05-01'
      and '2019-07-17'
      and Entidade = 'VD4'
      and tipodoc = 'VD'
      and CDU_SimbolBook = '*'
    ORDER BY
      RowNum ASC
  ) CabecDoc