【发布时间】:2023-03-28 03:55:02
【问题描述】:
我想迭代地更新每一行,当行更新时它应该使用以前最新的更新值,但是通过使用 CTE 方法来避免游标
光标伪代码:
Cursor{
--when cursor on row 1
update @table
set Balance=dbo.somefunction(sum of balance before row<1 i.e 0)
--when cursor on row 2
update @table
set Balance=dbo.somefunction(sum of balance before row<2 i.e 950)
.
.
.
};
Declare @table table(id int,col int,col2 int,balance int);
INSERT INTO @table
values(1,200,50,0),(2,60,150,0),(3,250,3,0),(4,65,2,0);
最终结果如下所示:
1 200 50 950
2 60 150 1
3 250 3 3
4 65 2 50
【问题讨论】:
-
您是否意识到您想要的结果与样本数据不一致?那 950 是从哪里来的?。
-
您为什么使用 SQL Server 2008?它不再受支持。
标签: sql sql-server-2008 common-table-expression