27,579
社区成员
发帖
与我相关
我的任务
分享
CREATE PROCEDURE sales_temp2
(
@dh varchar(11) --注:@dh 为销售或进货等的单号。。
)
AS
SET NOCOUNT ON
SET XACT_ABORT ON
BEGIN TRANSACTION
declare @is_2 varchar(6) --注:@is_2 的值为“销售单”,‘销退单','入库单','销退单'四个值中的一个
select @is_2=is_2 from 单头 where 单号=@dh
select a.分店,b.编码,sum(qty) 数量,IsNew=1 into #Sales from 单头 a,单体 b where a.单号=b.单号 and a.单号=@dh group by a.分店,b.编码
update a set a.IsNew = 0 from #Sales a join 库存表 b on a.分店=b.分店 and a.编码=b.编码
if @is_2='销售单' or @is_2='退库单'
begin
insert 库存表(分店,编码,库存数) select 分店,编码,0-数量 from #Sales where IsNew = 1
update a set a.库存数=a.库存数-b.数量 from 库存表 a join #Sales b on a.分店=b.分店 and a.编码=b.编码 where b.IsNew = 0
end
if @is_2='入库单' or @is_2='销退单'
begin
insert 库存表(分店,编码,库存数) select 分店,编码,数量 from #Sales where IsNew = 1
update a set a.库存数=a.库存数+b.数量 from store_total2 join #Sales b on a.分店=b.分店 and a.编码=b.编码 where b.IsNew = 0
end
drop table #Sales
COMMIT TRANSACTION
SET XACT_ABORT OFF
SET NOCOUNT OFF
GO