--示例--示例数据
create table tb(货物名 varchar(10),数量 int,日期 datetime)
insert tb select '电脑',10,'2005-01-01'
union all select '电脑',20,'2005-02-10'
union all select '电脑',10,'2005-03-04'
godeclare @num int
set @num=35 --要扣减的电脑数--扣减处理
declare @j int
update tb set
@j=case when 数量<@num then 数量 else @num end,
数量=数量-@j,@num=@num-@j
where isnull((select sum(数量) from tb a where a.日期<tb.日期),0)<@num
select * from tb
go--删除测试
drop table tb/*--结果货物名 数量 日期
---------- ----------- ---------------------------
电脑 0 2005-01-01 00:00:00.000
电脑 0 2005-02-10 00:00:00.000
电脑 5 2005-03-04 00:00:00.000(所影响的行数为 3 行)--*/
create table tb(货物名 varchar(10),数量 int,日期 datetime)
insert tb select '电脑',10,'2005-01-01'
union all select '电脑',20,'2005-02-10'
union all select '电脑',10,'2005-03-04'
godeclare @num int
set @num=35 --要扣减的电脑数--扣减处理
declare @j int
update tb set
@j=case when 数量<@num then 数量 else @num end,
数量=数量-@j,@num=@num-@j
where isnull((select sum(数量) from tb a where a.日期<tb.日期),0)<@num
select * from tb
go--删除测试
drop table tb/*--结果货物名 数量 日期
---------- ----------- ---------------------------
电脑 0 2005-01-01 00:00:00.000
电脑 0 2005-02-10 00:00:00.000
电脑 5 2005-03-04 00:00:00.000(所影响的行数为 3 行)--*/
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货