declare @ta table(row_id int,time varchar(10), amount numeric(12,2), status varchar(8))
insert @ta select
1 ,'10:20', 20.00 ,'yes' union select
2 ,'10:21', 30.00 ,'no' union select
3 ,'10:22', 40.00 ,'no' union select
4 ,'10:23', 60.00 ,'no'
declare @a int
set @a = 70
update @ta
set status = case when @a <= -amount then 'no' else 'yes'end ,
@a = @a -amount
where status = 'no'
select * from @ta
/*
row_id time amount status
----------- ---------- -------------- --------
1 10:20 20.00 yes
2 10:21 30.00 yes
3 10:22 40.00 yes
4 10:23 60.00 no
(所影响的行数为 4 行)
*/