发一个在用的进销存的存储过程,请大家提提意见!

xiaolongmao 2008-04-12 10:36:19

CREATE PROCEDURE sales_temp2(@dh varchar(11) ) AS 注:@dh 为销售或进货等的单号。。

begin transaction

declare @is_2 varchar(6)
declare @dh2 char(1)

select @is_2=is_2 from 单头 where 单号=@dh 注:@is_2 的值为“销售单”,‘销退单','入库单','销退单'四个值中的一个

select @dh2=dh2 from 单头 where 单号=@dh 注:@dh2 的值为“分店”


select a.分店,b.编码,sum(qty) 数量 into #lgj #lgj 为临时表,随便起的名
from 单头 a,单体 b where a.单号=b.单号 and a.单号=@dh
and a.分店=@dh2
group by a.分店,b.编码


//上面为汇总所有同笔单号的数量,按分店及编码汇总。


select a.分店,a.编号,a.sales_qty 数量 into #old_sales from #lgj a, 库存表 b
where a.分店=b.分店 and a.编码=b.编码

//查找库存表中与该笔单相匹配的编号及分店

select * into #news_sales from #lgj where 分店+编码 not in(select 分店+编码 from #old_sales )



if @is_2='销售单' or @is_2='退库单'
begin

insert 库存表(分店,编码,库存数)
select 分店,编码,0-数量
from #news_sales //插入新记录


update 库存表
set 库存数=库存数-数量
from #old_sales
where 库存表.编码=#old_sales.编码
and 库存表.分店=#old_sales.分店 //更新库存表库存

end

if @is_2='入库单' or @is_2='销退单'
begin

insert 库存表(分店,编码,库存数) //插入新记录
select 分店,编码,数量
from #news_sales

update store_total2
set 库存数=库存数+数量
from #old_sales
where 库存表.编码=#old_sales.编码
and 库存表.分店=#old_sales.分店

end

drop table #lgj
drop table #old_sales
drop table #news_sales


commit
GO
...全文
90 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
kaikai_kk 2008-04-12
  • 打赏
  • 举报
回复
不是正在用吗?
还有错的啊 ...
Limpire 2008-04-12
  • 打赏
  • 举报
回复
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
dichun 2008-04-12
  • 打赏
  • 举报
回复
主要不懂你业务,
把UPDATE 语句改一下;
update a
set a.sname = b.nane
frmo test1 a ,test b
where a.id = b.id
xiaolongmao 2008-04-12
  • 打赏
  • 举报
回复
楼上的,帮我看看这个存储过程有没有什么错误的地方呀,谢了~~
-狙击手- 2008-04-12
  • 打赏
  • 举报
回复
好长,顶一下

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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