我正在尝试使用此脚本:
UPDATE tablename
SET tablecolumn = abs(checksum(NewId()) % 100 + 1)
WHERE ID between 162 and 166
我要做的是将第162行更新为166,类似于
162 1
163 2
164 3
165 4
166 5
答案 0 :(得分:3)
您可以使用ROW_NUMBER()生成系列。但是SET column = ROW_NUMBER() ...
不起作用。相反,您需要在内联视图(或CTE)中选择值,然后更新值
UPDATE t
SET t.tablecolumn = t.tablecolumn_new
FROM (
SELECT id, tablecolumn, ROW_NUMBER() OVER (ORDER BY [id]) AS tablecolumn_new
FROM tablename
WHERE id between 162 and 166
) t;
答案 1 :(得分:0)
以防万一有人不顾此事,而只需要一个特定的值...将SQL信用归功于@Conrad Frix。
UPDATE t
SET t.tablecolumn = 100 --Changed this
FROM (
SELECT id, tablecolumn --Changed this
FROM tablename
WHERE id between 162 and 166
) t;