SQLServer语法(要求不使用游标)
单表T
字段 a(int) b(int) c(int) d(int)
1 0 0 10
2 10 5 Null
3 20 10 Null
4 30 15 Null
a字段自增,所需结果如下
字段 a(int) b(int) c(int) d(int)
1 0 0 10
2 10 5 15
3 20 10 25
4 30 15 40
过程 根据a自增, 累加d(初始为10) d=d+(b-c)
单语句Update如何写
update T
set d=(select isnull(sum(b-c),0) from T TT where TT.a<=T.a)+(select d from T where a=1)
a b c d
----------- ----------- ----------- -----------
1 0 0 10
2 10 5 15
3 20 10 25
4 30 15 40