将列值增加一

时间:2017-01-07 20:29:50

标签: sql sql-server sql-server-2012

我正在尝试更新一个列(int)并且它没有像我想要的那样递增1,但它每8行递增一次。所以这样做:

表1

ID  FIELD_NUMBER
----------------
1    3507572
2    3507572
3    3507572
4    3507572
5    3507572
6    3507572
7    3507572
8    3507572
9    3507573
10   3507573
11   3507573
12   3507573
13   3507573
14   3507573
15   3507573
16   3507573

它应该增加1 3507572,3507573等

代码:

DECLARE @id INT 
SET @id = 3507571 

UPDATE table1
SET @id = FIELD_NUMBER = @id + 1 
GO

不确定原因。我正在使用SQL Server 2012.想法?

1 个答案:

答案 0 :(得分:3)

更好的方法是使用可更新的CTE:

DECLARE @id INT;
SET @id = 3507571;

with toupdate as (
      select t1.*, row_number() over (order by field_number) as seqnum
      from table1 t1
     )
update toupdate
    set field_number = @id + seqnum;