库管易

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫描二维码登录本站

查看: 36447|回复: 40

进销存软件库存管理设计,查询任何时间出入库数量及结存数

  [复制链接]
  在ERP或进销存软件设计时,常用常用需要知道某时的期初库存及期末库存,因而需要记录库存的实时进出。

  设计一个表用于记录库存的实时进出。
  1. --库存表,用于存放当前库存

  2. IF OBJECT_ID ('Storage',N'U') IS NOT NULL
  3. drop table Storage
  4. create table Storage
  5. (
  6.  ID int identity(1,1),
  7.  ProductID int not null,--物品ID
  8.  StorageID int not null,--仓库ID
  9.  Amount decimal(8,2)--当前数量
  10. primary key(ProductID,StorageID)
  11. )

  12. --库存进出记录表,用于记录仓库的进出流水帐

  13. IF OBJECT_ID ('StorageInOut',N'U') IS NOT NULL
  14. drop table StorageInOut
  15. create table StorageInOut
  16. (
  17.  ID int identity(1,1),
  18.  InOutDate datetime default getdate() not null,--进出时间
  19.  ProductID int not null,--物品ID
  20.  StorageID int not Null,--仓库ID
  21.  StorageIn decimal(8,2) default 0 not null,--进库数量
  22.  StorageOut decimal(8,2) default 0 not null,--出库数量
  23.  Amount decimal(8,2) default 0 not null--结存数量
  24. )
复制代码

  再在库存表Storage上创建一个触发器,用于把进出数据写入StorageInOut表中。
  1. IF OBJECT_ID ('StorageLog','TR') IS NOT NULL
  2.    DROP TRIGGER StorageLog
  3. GO
  4. CREATE TRIGGER StorageLog
  5. ON Storage
  6. AFTER INSERT, UPDATE
  7. AS
  8. declare @ProductID int,
  9.  @StorageID int,
  10.  @Old decimal(8,2),
  11.  @New decimal(8,2),
  12.  @Amount decimal(8,2)
  13. select @Old=Amount from deleted
  14. select @ProductID=ProductID,@StorageID=StorageID,@New=Amount from inserted

  15. select @Amount=Amount from StorageInOut where ID = (select Max(ID)
  16. from StorageInOut where ProductID=@ProductID and StorageID=@StorageID)
  17. if @Amount is null
  18.    set @Amount=0
  19. if @Old is not null
  20. begin
  21. if @New >@Old
  22. begin
  23.  insert StorageInOut(ProductID, StorageID,StorageIn, StorageOut,Amount)
  24.               values(@ProductID,@StorageID,@New-@Old,0,@Amount+@New-@Old)
  25. end else
  26. begin
  27.  insert StorageInOut(ProductID, StorageID,StorageIn, StorageOut,Amount)
  28.               values(@ProductID,@StorageID,0, @Old-@New,@Amount-@Old+@New)
  29. end
  30. end else
  31. begin
  32. insert StorageInOut(ProductID, StorageID,StorageIn, StorageOut,Amount)
  33.               values(@ProductID,@StorageID,@New, 0,@Amount+@New)
  34. end
  35. GO
复制代码

  取某时段的期初库存量:
  1. select * from StorageInOut where id=(select max(id) from StorageInOut where InOutDate<'2012-10-11 22:36:44')
复制代码

  取某时段的出入库数量:
  1. select sum(storageOut) '出库量',sum(StorageIn) '入库量' from StorageInOut where InOutDate between '2012-10-11 22:36:44' and '2012-12 :23:59:59'
复制代码

  取某时段结存(期末)库存量:
  1. select * from StorageInOut where id=(select max(id) from StorageInOut where InOutDate between '2012-10-11 22:36:44' and '2012-12 :23:59:59')
复制代码

  这样就可以查询到任何时间的出入库数量及结存数,对移动盘点,库存管理就方便多了。

  附:Sqlite 创建触发器的方法
  1. --创建班级表
  2. create table class
  3. (
  4.     id integer primary key autoincrement, --班级编号
  5.     className nvarchar(50) --班级名称
  6. );

  7. --创建学生表
  8. create table student
  9. (
  10.     id integer primary key autoincrement, --编号
  11.     stuName nvarchar(20), --学生名称
  12.     stuSex bit, --性别
  13.     stuAge integer , --年龄
  14.     classId --班级编号
  15. );

  16. --创建插入触发器 (创建学生时要触发插入触发器去判断是否存在该班级,存在插入成功,反之插入失败)
  17. create trigger fk_Insert
  18. before insert on student
  19. for each row
  20. begin
  21.     select raise(rollback,'还没有该班级')
  22.     where (select id from class where id = new.classId ) is null;
  23. end;

  24. --创建更新触发器 (更新学生时要触发更新触发器去判断是否存在更新班级,存在更新成功,反之更新失败)
  25. create trigger fk_Update
  26. before update on student
  27. for each row
  28. begin
  29.     select raise(rollback,'还没有该班级')
  30.     where (select id from class where id = new.classId)is null;
  31. end;

  32. --创建删除触发器 (删除班级时,首先根据班级编号删除该班级学生)
  33. create trigger fk_Delete
  34. before delete on class
  35. for each row
  36. begin
  37.      delete from student where classId = old.classId;
  38. end ;


  39. insert into class(className) values('s1t64');
  40. insert into student(stuName,stuSex,stuAge,classId)values('zhangsan',1,23,1);
  41. update student set stuName='lishi',classId=1 where id = 1;

  42. select * from class ;
  43. select * from student limit 0,100 ; -- 分页查询从索引0开始查找,100条数据
复制代码

评分

参与人数 1金钱 +3 收起 理由
4875720 + 3 说的很有道理,赞一个

查看全部评分

回复

使用道具 举报

好,先学习了
回复 支持 反对

使用道具 举报

谢谢。好,先学习了
回复 支持 反对

使用道具 举报

谢谢。好,先学习了
回复 支持 反对

使用道具 举报

学习学习,感谢楼主分享
回复 支持 反对

使用道具 举报

东西不错    学习学习  
回复 支持 反对

使用道具 举报

谢谢学习学习
回复 支持 反对

使用道具 举报

学习!
长了不少见识。
回复 支持 反对

使用道具 举报

有点,复杂,。 学习一下
回复 支持 反对

使用道具 举报

学习了。谢谢!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|仓库管理网

GMT+8, 2024-4-20 14:35

Powered by 库管易

KuGuanYi.Com

快速回复 返回顶部 返回列表