当前位置: 代码迷 >> Sql Server >> 求触发器实现按时间统计仓储余额,该怎么处理
  详细解决方案

求触发器实现按时间统计仓储余额,该怎么处理

热度:111   发布时间:2016-04-27 12:38:02.0
求触发器实现按时间统计仓储余额
一个项目中,每天有货物进出。
我弄了二个表,一个是流水表,一个是余额表。余额表用触发器管理
当有货物进出时,写入在流水表上,同时余额表反映其变化。并要保留时间信息。
流水表的结构是:序号,货物名称,进出方向,货物进出量,货物进出时间
余额表是:货物名称,货物余额,货物进出时间。

当进货仓储增加时,直接在余额表上增加。如果是同一天的,余额表上余额加在一起
当出货追念减少时,余额表上按先进先出的原则,如果余额0,则不显示此货物

如仓库余额表里原有:
轴承 100只 5月1日
端盖 200只 6月1日
轴承 200只 7月1日
轴承 330只 8月1日
轴承 400只 9月1日

当9月1日增加轴承50只时,9月2日增加端盖30只时表变成
轴承 100只 5月1日
端盖 200只 6月1日
轴承 200只 7月1日
轴承 330只 8月1日
轴承 450只 9月1日
端盖 30只 9月2日

当9月3日出货轴承400只时,即是
轴承 0只 5月1日
端盖 200只 6月1日
轴承 0只 7月1日
轴承 230只 8月1日
轴承 450只 9月1日
端盖 30只 9月2日

其中0只不显示,表应为
端盖 200只 6月1日
轴承 230只 8月1日
轴承 450只 9月1日
端盖 30只 9月2日

求触发器实现上述余额表。谢谢



------解决方案--------------------
SQL code
--分析没错,不知执行有没有错,没建表,试下create trigger [流水表_updins]on [流水表]for insertasupdate 余额表    set 货物余额=货物余额+i.货物进出量    from inserted i    where 余额表.货物名称=i.货物名称        and i.进出方向='进货'        and convert(varchar(10),i.货物进出时间,111)=convert(varchar(10),余额表.货物进出时间,111)insert into 余额表    select 货物名称,货物进出量,货物进出时间    from inserted i    where not exists (select 1 from 余额表        where 余额表.货物名称=i.货物名称            and convert(varchar(10),i.货物进出时间,111)=convert(varchar(10),余额表.货物进出时间,111))         and i.进出方向='进货'--你的出货时间就按晚于所有进货的来计算declare @proname nvarchar(50),@qty floatdeclare cur cursor local for select 货物名称,货物进出量=sum(货物进出量) from inserted i where i.进出方向='出货'open curfetch next from cur into @proname,@qtywhile @@fetch_status=0begin    declare @tmpqty float,@tmpdate varchar(10)    while exists(select 1 from 余额表 where 货物进出量>0) and @qty>0    begin        select top 1 @tmpqty=货物进出量,@tmpdate=convert(varchar(10),货物进出时间,111)             from 余额表            where 货物进出量>0 and [email protected]            order by 货物进出时间 asc        if(@qty>@tmpqty)        begin            delete 余额表 where [email protected] and convert(varchar(10),货物进出时间,111)[email protected]            select @qty=@qty-@tmpqty        end        else        begin            update 余额表 set [email protected] where [email protected] and convert(varchar(10),货物进出时间,111)[email protected]            delete 余额表 where [email protected] and convert(varchar(10),货物进出时间,111)[email protected] and 货物进出量=0            select @qty=@qty-@tmpqty        end    end    fetch next from cur into @proname,@qtyendclose curdeallocate curgo
------解决方案--------------------
SQL code
create trigger tri_l_u_ion liusfor insertasdeclare @type char(2),@total int,@date datetime,@cName char(10),@con int,@i int ,@t intselect @cName=cName,@total=total,@date=date,@type=ctype from insertedif @type='入'begin    if exists(select 1 from yue where [email protected] and [email protected])        update yue set [email protected] where [email protected] and [email protected]    else        insert into yue values(@cName,@total,@date)endif @type='出'begin    select @t=sum(total) from yue where [email protected]    if @total>@t        return    while @total>0    begin        select top 1 @con=total,@date=date from yue where [email protected] order by date asc        if @con>[email protected]        begin            select @con=@con-@total,@total=0            update yue set [email protected] where [email protected] and [email protected]        end        else        begin            select @total=@total-@con,@con=0            delete yue where [email protected] and [email protected]        end    endend------------------------------------------测试表结构:create table yue(    cName char(10),    total int,    date datetime)create table lius(    cName char(10),    total int,    date datetime,    ctype char(2))insert into yue select '轴承','100','2012-05-01' union allselect '端盖','200','2012-06-01' union allselect '轴承','200' ,'2012-07-01'union allselect '轴承', '330', '2012-08-01'union allselect '轴承', '400', '2012-09-01'-------------------------------------------触发器触发语句:insert into lius select  '轴承', '10', '2012-09-7','入'insert into lius select  '轴承', '9', '2012-09-8','入'insert into lius select  '轴承', '10', '2012-09-9','入'insert into lius select  '轴承', '27', '2012-09-10','出'
  相关解决方案