存储过程作业

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go



-- 商品出货
-- 返回: -1 数据 overflow
ALTER PROCEDURE [dbo].[ap_PopPro]
@pro_id int, --产品ID
@wh_id int, --仓库号
@num int --出库数量

AS
BEGIN
declare
@pos1 int, --起始位置
@pos2 int, --可放位置
@curPos int, --当前位置
@totNum int, --总的数量
@curNum int , --当前位置的数量
@curtotNum int, --当前商品总数量
@totPrice money, --总的出货价
@curPrice money --当前批的出货价

declare
@sqlCmd nvarchar(200),
@fldNum varchar(10),
@fldPrice varchar(10)
--初始化出货总价
set @totPrice = 0

--查找对应产品的记录
select @pos1 = pos1, @pos2 = pos2, @totNum = totNum
from whproduct
where pro_id = @pro_id and wh_id = @wh_id

if @totNum < @num --产品数量不够,刚退出
begin
print 'The number of the product is not enough'
return -1
end

laber1:
set @fldNum = 'num' + Convert(varchar(2), @pos1) --得到当前num字段
set @fldPrice = 'price' + Convert(varchar(2), @pos1)
set @sqlCmd = 'select ' + '@curNum ='+ @fldNum+ ' ,@curtotNum = totNum'
+ ',@curPrice='+@fldPrice +' from whproduct where pro_id = @pro_id and wh_id = @wh_id'
exec sp_executesql @sqlCmd,N'@curNum int output ,@curtotNum int output, @curPrice money output ,@pro_id int,@wh_id int',@curNum output ,@curtotNum output,@curPrice output,@pro_id,@wh_id

set @curPos = @pos1 --一开始的初始化curPos 与@pos1相等,用于后面价格是否需要置0做准备
if @curNum >=@num --当前位置的商品数量大于或等于要求的数量时
begin
set @curNum = @curNum - @num
set @curtotNum = @curtotNum - @num
set @totPrice = @totPrice + @curPrice*@num
set @num = 0 --取出数量全总,所以设为0


if @curNum = 0
begin
set @pos1 = @pos1 +1
end
end
else --当前位置的商品数量小于要求的数量时
begin
set @num = @num - @curNum
set @totPrice = @totPrice + @curPrice*@curNum
set @curtotNum = @curtotNum - @curNum
set @curNum = 0
set @pos1 = @pos1 + 1

end

if @curPos != @pos1 --当发现当前位置的商品都被取光的时候,该位置的进货价格清0
begin
set @fldPrice = 'price' + Convert(varchar(2), @curPos)
set @sqlCmd = 'update whProduct set ' + @fldprice + ' = ' + Convert(varchar(10), 0)
+' where pro_id = @pro_id and wh_id = @wh_id'
exec sp_executesql @sqlCmd, N'@wh_id int, @pro_id int', @wh_id, @pro_id
end

if @pos1 = @pos2 --当位置1到达位置2时,也就相当于仓库里面没货了,这时应该把@pos1返回
begin
set @pos1 = 1
set @pos2 = 1
end

--当前位置被置0后,修改数据表中的值

set @sqlCmd = 'update whProduct set pos1 = ' + Convert(varchar(2), @pos1) + ',pos2 = ' + Convert(varchar(2), @pos2) +', totNum = '
+ Convert(varchar(5), @curtotNum) + ',' + @fldNum +'='+ Convert(varchar(5), @curNum)+
' where pro_id = @pro_id and wh_id = @wh_id'
exec sp_executesql @sqlCmd, N'@wh_id int, @pro_id int', @wh_id, @pro_id


if @num != 0
begin
goto laber1 --如果需要的商品数量还没有得到,则回到前面部分,进而得到循环的目的
end

print '出货成功--'
print '出货数量为:'
print @totNum - @curtotNum
print '出货总价为:'
print @totPrice

END
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值