日期:2014-05-18  浏览次数:20694 次

求一个存储过程???????在线等!!!!
现在我有三个表:   storage,instorage和outstorage
分别包含的字段为:
storage:id,materialid,amount
instorage:instorageid,materialid,amount,instoragedate
outstorage:outstoageid,mateiralid,amount,outstoragedate
现在要做一个月报表,形式为
storage里面所有的物资,上月库存数量,这个月进库数,出库数,现库存数量
我现在做法是每次都要取storage里面的materialid,然后在instorage和outstorage进行查找是否存在进库和出库,这样后台就要运算很长的时间(因为涉及到的物资有上万种),能不能用个存储过程然后从存储过程里面直接去出来呢?


------解决方案--------------------
select 物料,
上月库存数量=amount+(select isnull(sum(i.amount),0)-isnull(sum(o.amount),0) from instorage i,outstorage o where i.materialid=o.materialid and a.materialid=i.materialid and datediff(m,i.instoragedate,dateadd(mm,-1,getdate()))=1 and datediff(m,o.outstoragedate,dateadd(mm,-1,getdate()))=1)

这个月进库数=(select isnull(sum(i.amount),0) from instorage i where a.materialid=i.materialid and datediff(m,i.instoragedate,getdate())=1)

出库数=(select isnull(sum(o.amount),0) from outstorage o where a.materialid=o.materialid and datediff(m,o.outstoragedate,getdate())=1)

现库存数量==(select isnull(sum(i.amount),0) from instorage i where a.materialid=i.materialid and datediff(m,i.instoragedate,getdate())=1)-=(select isnull(sum(o.amount),0) from outstorage o where a.materialid=o.materialid and datediff(m,o.outstoragedate,getdate())=1)
from storage a
order by 物料
------解决方案--------------------
这种东西还是自己写吧,都是~``
------解决方案--------------------
解决了吗?没解决的话加我QQ 195428528聊
------解决方案--------------------
比较复杂,:) 帮楼主顶了
------解决方案--------------------
这种月报表都是这样的吧……除非你有专门的库存表,也就是包含期出数、本月入库、本月初库、剩余量、月份这样的库存表,能够直接查出来

否则LZ这样的三个表要求出这个月报表来,都是需要SQL语句来查询的

至于用存储过程,那也不过是将这些语句放在存储过程中而已,具体的效率问题,都还在你的SQL语句,以及给表建立适当的索引来提高查询的效率

呵呵
------解决方案--------------------
我帮忙顶一下..`
..小弟也想看看解决方案