SQL Server(T-sql) 从下往上更新table

SQL Server(T-sql) Update table from botom to top

我正在尝试从下到上更新 table temp(F4) 结果。 是不是每一行都在while循环并且更新可以排序?

谢谢!

假设ROW是一个正整数那么你可以使用(demo)

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);