关于仓库的先进先出

--问题
进货表结构:
连接:http:
//topic.csdn.net/u/20080825/20/c81e3462-942b-40d5-971b-b7b94f15590f.html


进货编号        品名        单价                    进货数       
----------- ---------- --------------------- -----------
1001        aa        500.0000              5
1002        aa        400.0000              10
1002        bb        200.0000              2
1003        aa        500.0000              20
1004        bb        300.0000              20

出货表结构:

出货编号        品名        出货数       
----------- ---------- -----------
2001        aa        11
2001        bb        10
2001        aa        20
2001        aa        1

结果表:

出货编号    品名        出货数      单价   
----------- ---------- -----------
2001        aa        5            500.0000       
2001        aa        6            400.0000
2001        aa        4            400.0000
2001        aa        16            500.0000
2001        aa        1            500.0000
2001        bb        2            200.0000   
2001        bb        8            300.0000         


说明:出货表根据进货表的情况,把出货数量拆分出来匹配每次进货的价格,先进先出,本次出货可能对应多次进货,把他们都拆分出来


建表环境:

create table 入库表(进货编号 int,品名 varchar(10),单价 money,进货数 int)
insert 入库表 select 1001,'aa',500,5
union all select 1002,'aa',400,10
union all select 1002,'bb',200,2
union all select 1003,'aa',500,20
union all select 1004,'bb',300,20

create table 出库表(出货编号 int,品名 varchar(10),出货数 int)
insert 出库表 select 2001,'aa',11
union all select 2001,'bb',10
union all select 2001,'aa',20
union all select 2001,'aa',1
go
--徐王锦2008/12/31 pm 23:00--
----解决方法
--
1.需要一个自增加的id列来判断先后
select id=identity(int,1,1),* into 入库表2 from 入库表
select id=identity(int,1,1),* into 出库表2 from 出库表
--2
set nocount on
declare @id int,@品名 varchar(200),@出货数量 int,@单价 money,@出货编号 varchar(200)
declare @进货数 int,@pbid int ,@maxid int,
if not exists (select * from 出库表2) return
set @id=1
select @maxid=max(id) from 出库表2
while @id <=@maxid
begin
select @品名=品名,@出货编号=出货编号,@出货数量=出货数
from (select * from 出库表2 where id=@id)x
if exists(select * from 入库表2 where 品名=@品名 and 进货数>0 )
begin
 
if exists(select top 1 * from 入库表2 where 品名=@品名  order by id)
 
begin
 
select @进货数=进货数,@pbid=id from
   (
select top 1 * from 入库表2 where 品名=@品名 and 进货数>0 order by id) x
 
if @出货数量 <=@进货数
 
begin
  
select @单价=单价
   
from (select top 1 * from 入库表2 where 品名=@品名 and 进货数>0  order by id)x
  
print @出货编号 +','+ @品名+','+cast(@出货数量 as varchar(200))+','+cast(@单价 as varchar(200))
  
update 入库表2 set 进货数=进货数-@出货数量 where id=@pbid  and 品名=@品名
 
end
 
else if @出货数量>@进货数
 
begin
  
select @单价=单价,@进货数=进货数
   
from (select top 1 * from 入库表2 where 品名=@品名 and 进货数>0  order by id)x
  
print @出货编号 +','+ @品名+','+cast(@进货数 as varchar(200))+','+cast(@单价 as varchar(200))
  
update 入库表2 set 进货数=0 where id=@pbid and 品名=@品名
  
declare @j int,@差额数量 int
  
set @j=@pbid
  
select @差额数量=@出货数量-@进货数
   
from (select top 1* from 入库表2 where 品名=@品名 and 进货数>0  order by id)x
  
while @差额数量>0
  
begin
   
IF NOT exists(select top 1 * from 入库表2 where 品名=@品名 and 进货数>0  and id>@j order by id)
   
begin
    
return
   
end
   
select  @出货数量=进货数 ,@单价=单价,@pbid=id
    
from  (select top 1 * from 入库表2 where 品名=@品名 and 进货数>0 and id>@j order by id)x
   
if @出货数量>=@差额数量
   
begin
    
print @出货编号 +','+ @品名+','+cast(@差额数量 as varchar(200))+','+cast(@单价 as varchar(200))
    
update 入库表2 set 进货数=进货数-@差额数量 where id=@pbid and 品名=@品名
    
select @差额数量=-1
   
end
   
else
   
begin
    
print @出货编号 +','+ @品名+','+cast(@出货数量 as varchar(200))+','+cast(@单价 as varchar(200))
    
set @差额数量=@差额数量-@出货数量
    
update 入库表2 set 进货数=0 where id=@pbid and 品名=@品名
   
end
   
set @j=@j+1
  
end
 
end
end
end
set @id=@id+1
end
set nocount off
/*
2001,aa,5,500.00
2001,aa,6,400.00
2001,bb,2,200.00
2001,bb,8,300.00
2001,aa,4,400.00
2001,aa,16,500.00
2001,aa,1,500.00
*/
--删除测试
drop table 入库表,出库表

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值