是否有T-SQL语句自动填充表中的空列,其值从一个特定值开始递增?
E.g。
UPDATE A SET A.NO = ROW_NUMBER() OVER (ORDER BY A.NO) + @max WHERE A.NO is NULL
这句话不起作用,我不知道如何让它运行......
感谢任何帮助!
答案 0 :(得分:3)
WITH q AS
(
SELECT a.*, MAX(no) OVER() + ROW_NUMBER() OVER (ORDER BY a.no) AS rn
FROM a
)
UPDATE q
SET no = rn
答案 1 :(得分:2)
这很有效。您需要将排名函数与更新
分离UPDATE
bar
SET
NO = bar.foo + @max
FROM
(SELECT
A.NO,
ROW_NUMBER() OVER (ORDER BY A.NO) AS foo
FROM
A
WHERE
A.NO is NULL
) bar