我试图从下到上更新表 temp(F4) 结果。是否可以对每一行进行while循环并且可以对更新进行排序?
谢谢!
假设ROW
是一个正整数,那么你可以使用(演示)
WITH T1 AS
(
SELECT *,
MIN(CASE WHEN F4 <> 0 THEN CONCAT(FORMAT([ROW],'D10'),[F4]) END)
OVER (ORDER BY [ROW] ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) AS M
FROM temp
)
SELECT *,
CAST(SUBSTRING(M,11,11) AS INT) AS val
FROM T1
ORDER BY [ROW]
如果你想更新F4
你可以使用
WITH T1 AS
(
SELECT *,
MIN(CASE WHEN F4 <> 0 THEN CONCAT(FORMAT([ROW],'D10'),[F4]) END)
OVER (ORDER BY [ROW] ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) AS M
FROM temp
)
UPDATE T1
SET F4 = COALESCE(SUBSTRING(M,11,11),0);