批次循环扣减问题

Mr.Huang…… 2020-03-03 09:31:39


create table #处方明细表
(
处方id int,
序号 int,
药品id int,
数量 int
)
insert into #处方明细表
select 1,1,101,10 union all
select 1,2,102,20 union all
select 1,3,101,30 union all
select 1,4,103,40


create table #库存明细表
(
入库单号 int,
入库序号 int,
药品id int,
数量 int
)
insert into #库存明细表
select 1001,1,101,20 union all
select 1001,2,102,10 union all
select 1001,3,103,50 union all
select 1002,1,101,8 union all
select 1002,2,102,30 union all
select 1003,1,101,20

--得到结果 select * from #处方明细表
处方id 序号 药品id 数量 入库单号 入库序号 扣减数量
1 1 101 10 1001 1 10
1 2 102 20 1001 2 10
1 2 102 20 1002 2 10
1 3 101 30 1001 1 10
1 3 101 30 1002 1 8
1 3 101 30 1003 1 12
1 4 103 40 1001 3 40

根据处方明细查询出从哪些库存批次进行扣减,得出的结果中有多个序号是因为一个批次数量不足,从多个批次扣减的,处方中允许出现相同规格的药品,麻烦大佬些指点下,能不能不用游标,直接查询出结果
...全文
146 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mr.Huang…… 2020-03-10
  • 打赏
  • 举报
回复
别沉啊,大神些

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧