如何更新行列表中同一表中另一列的列?

时间:2018-12-23 12:01:25

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

我在Table A中具有以下数据结构:

RequestId  |  Serial  |  RowSerial
-----------+----------+----------
   1       |    1     |    NULL
   1       |    2     |    NULL
   1       |    3     |    NULL

我需要查询以进行以下更新:

RequestId  |  Serial  |  RowSerial
-----------+----------+----------
   1       |    1     |    501
   1       |    2     |    502
   1       |    3     |    503

500是添加到Serial列并设置为RowSerial列的静态数字。

我已经尝试过了:

UPDATE Table A
SET RowSerial=(SELECT top 1 500+(Serial) FROM Table A where requestid=1 and RowSerial is Null) where requestid=1

但是它没有用。该怎么做?

2 个答案:

答案 0 :(得分:2)

简单

UPDATE TableA
SET RowSerial = Serial + 500;

答案 1 :(得分:1)

一个简单的补充:

UPDATE TableA
SET RowSerial = 500 + Serial
WHERE RequestId = 1 AND RowSerial IS NULL

仅在要将更新限制为特定行时才使用WHERE部分。