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
分享到:
相关推荐
MySQL存储过程作业6.docx
触发器、存储过程等上机作业 数据库SQL Server
阅读实验教材《SQL Server 2012数据库管理与开发》第137页到155页的内容,掌握存储过程的概念、了解存储过程的类别(系统存储过程,用户自定义存储过程,扩展存储过程);掌握存储过程的建立;掌握存储过程的两种...
MySQL实验报告5(存储过程与函数)(1)(1).pdf
黑笔是我写的,红笔是我在网上找的答案。我俩参考表设置的不一样,我设置反了! 他这个语句好像也不是标准SQL,应该是MYSQL还是Oracle吧。希望老师解答~ (1)统计离散数学成绩分布 CREATE TABLE SCC( Grades CHAR(5...
视图,触发器,存储过程,ER图.sqlserver数据库实训,广告公司管理系统,sqlsever数据库大作业,包括增删改查。。视图,触发器,存储过程,ER图.sqlserver数据库实训,广告公司管理系统,sqlsever数据库大作业,包括...
自动备份SQL SERVER数据库的作业的脚本及存储过程(学习)
存储过程中查找关键字,存储过程,关键字,查询存储过程关键字
11.6.3 用于维护作业的存储过程 11.6.4 操作员和报警 11.7 SQL Server和Web 11.7.1 Web Assistan 11.7.2 Web任务存储过程 11.7.3 sp_makewebtas 11.7.4 sp_runwebtas 11.7.5 sp_dropwebtas 11.7.6 Web页模板 11.8 串...
存储过程 自定义函数 作业 - 参考答案.sql
教程\存储过程和触发器.ppt
设计一个可变式分区分配的存储管理方案。并模拟实现分区的分配和回收过程。假设初始状态下,可用的内存空间为640KB,并有下列的请求序列: 作业1申请130KB 作业2申请60KB 作业3申请100KB 作业2释放60KB 作业4申请200...
->命令 [ 如果是简单的SQL直接写进去即可,也可用打开按钮输入一个已写好的*.sql文件 如果要执行存储过程,填 exec p_procedure_name v_parameter1,[ v_parameter2…v_parameterN] ] ...
存储过程-作业1-利思源(6)-参考答案.sql
SQL存储过程与事务
基于SQL Server作业 扩展存储过程实现数据库自动远程备份.pdf
第8章 存储过程的操作与管理 sql server
用C语言语言模拟一个作业的执行过程,该作业共有320条指令, 即它的地址空间为32页,目前它的所有页都还未调入内存。在模拟过程中,如果所访问的指令已在内存,则显示其物理地址,并转下一条指令。如果所访问的指令...
2、系统架构设计是否良好,运行过程是否报错 3、界面设计的合理性和美观程度 4、基本功能的实现 分值60 (包括布局、组件、Activity、Intent等使用) 数据存储的使用 分值10 网络功能 ...