create table t(id int,num int)
insert into t select 1,200
insert into t select 2,10
insert into t select 3,60
insert into t select 4,80
insert into t select 5,600
delete from t
declare @sum int
set @sum=300
update a
set
num=(case when (select sum(num) from t where id<=a.id)< @sum
then 0
when
(select sum(num) from t where id< a.id)< @sum
and
(select sum(num) from t where id<=a.id)>=@sum
then
(select sum(num) from t where id<=a.id)-@sum
else a.num end)
from
t a
select * from t
/*
id num
----------- -----------
1 0
2 0
3 0
4 50
5 600
*/
雖然很普通,但讓人啓發很大
覺得寫的比較好的語句(UPDTAE)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.