我的SQL存储过程如下:
alter procedure SpExceedStock
@NumberId nvarchar(20),--单据凭证编号
@SupplierID int,--供应商
@RegimentationID int,--入库类别
@ProductID int,--产品ID
@ProductName nvarchar(50),--产品名称
@Spec nvarchar(20),--规格
@Unit nvarchar(20),--单位
@Quantity int,--数量
@Price float,--单价
@TotalPrice float,--总金额
@DepotID int,-- 本地仓库
@DepotName nvarchar(20),
@UDepotID int ,--提货仓库
@State int,--仓库:为入库为出库
@Operate nvarchar(20),--操作员
@StockDate datetime,
@Re nvarchar(50)--备注
AS
BEGIN
SET NOCOUNT ON;
IF (@State=0)--出库
BEGIN
update tb_Stock1 set Quantity = Quantity-@Quantity,TotalPrice=TotalPrice-@TotalPrice where ProductID=@ProductID and DepotID=@DepotID
update tb_Stock1 set Quantity = Quantity+@Quantity,TotalPrice=TotalPrice+@TotalPrice where ProductID=@ProductID and DepotID=@UDepotID INSERT INTO tb_ParticularDepot --出入库表
(
NumberId,
SupplierID,
RegimentationID,
ProductID,
ProductName,
Spec,
Unit,
Quantity,
Price,
TotalPrice,
DepotID,
DepotName,
UDepotID,
State,
Operate,
StockDate,
Re
)
VALUES
(
@NumberId ,--单据凭证编号
@SupplierID ,--供应商
@RegimentationID ,--入库类别
@ProductID ,--产品ID
@ProductName ,--产品名称
@Spec ,--规格
@Unit ,--单位
@Quantity ,--数量
@Price ,--单价
@TotalPrice ,--总金额
@DepotID ,-- 接收仓库
@DepotName,
@UDepotID,
@State ,--仓库:为入库为出库
@Operate ,--操作员
@StockDate ,
@Re --备注
)
END
END
GO问题:为什么一直不执行下列语句,请大家指正一下如果,我一定要执行下列语句:
IF (@State=0)--出库
BEGIN
update tb_Stock1 set Quantity = Quantity-@Quantity,TotalPrice=TotalPrice-@TotalPrice where ProductID=@ProductID and DepotID=@DepotID
update tb_Stock1 set Quantity = Quantity+@Quantity,TotalPrice=TotalPrice+@TotalPrice where ProductID=@ProductID and DepotID=@UDepotID
alter procedure SpExceedStock
@NumberId nvarchar(20),--单据凭证编号
@SupplierID int,--供应商
@RegimentationID int,--入库类别
@ProductID int,--产品ID
@ProductName nvarchar(50),--产品名称
@Spec nvarchar(20),--规格
@Unit nvarchar(20),--单位
@Quantity int,--数量
@Price float,--单价
@TotalPrice float,--总金额
@DepotID int,-- 本地仓库
@DepotName nvarchar(20),
@UDepotID int ,--提货仓库
@State int,--仓库:为入库为出库
@Operate nvarchar(20),--操作员
@StockDate datetime,
@Re nvarchar(50)--备注
AS
BEGIN
SET NOCOUNT ON;
IF (@State=0)--出库
BEGIN
update tb_Stock1 set Quantity = Quantity-@Quantity,TotalPrice=TotalPrice-@TotalPrice where ProductID=@ProductID and DepotID=@DepotID
update tb_Stock1 set Quantity = Quantity+@Quantity,TotalPrice=TotalPrice+@TotalPrice where ProductID=@ProductID and DepotID=@UDepotID INSERT INTO tb_ParticularDepot --出入库表
(
NumberId,
SupplierID,
RegimentationID,
ProductID,
ProductName,
Spec,
Unit,
Quantity,
Price,
TotalPrice,
DepotID,
DepotName,
UDepotID,
State,
Operate,
StockDate,
Re
)
VALUES
(
@NumberId ,--单据凭证编号
@SupplierID ,--供应商
@RegimentationID ,--入库类别
@ProductID ,--产品ID
@ProductName ,--产品名称
@Spec ,--规格
@Unit ,--单位
@Quantity ,--数量
@Price ,--单价
@TotalPrice ,--总金额
@DepotID ,-- 接收仓库
@DepotName,
@UDepotID,
@State ,--仓库:为入库为出库
@Operate ,--操作员
@StockDate ,
@Re --备注
)
END
END
GO问题:为什么一直不执行下列语句,请大家指正一下如果,我一定要执行下列语句:
IF (@State=0)--出库
BEGIN
update tb_Stock1 set Quantity = Quantity-@Quantity,TotalPrice=TotalPrice-@TotalPrice where ProductID=@ProductID and DepotID=@DepotID
update tb_Stock1 set Quantity = Quantity+@Quantity,TotalPrice=TotalPrice+@TotalPrice where ProductID=@ProductID and DepotID=@UDepotID
也就是你传的值不是0,
@State int--仓库:为入库为出库
AS
BEGIN
SET NOCOUNT ON;
IF (@State=0)--出库
begin
print 'OK'
end
else
begin
print 'NG'
end
结果:
/*
OK
*/
所以,问题应该在于你的两个update语句不满足条件
update tb_Stock1 set Quantity = Quantity-@Quantity,TotalPrice=TotalPrice-@TotalPrice where ProductID=@ProductID and DepotID=@DepotID
相对应的数据库当中都有满足记录的条件...
是不是有null
Quantity = isnull(Quantity,0)-isnull(@Quantity,0)
可是Quantity 最小的值是0
估计我另外没有想到
感谢各位...
@NumberId nvarchar(20),--单据凭证编号
@ProductID int,--产品ID
@ProductName nvarchar(50),--产品名称
@Spec nvarchar(20),--规格
@Unit nvarchar(20),--单位
@Quantity int,--数量
@Price float,--单价
@TotalPrice float,--总金额
@DepotID int,-- 本地仓库
@DepotName nvarchar(20),
@UDepotID int ,--提货仓库
@State int,--仓库:入库为1,出库为0
@Operate nvarchar(20),--操作员
@StockDate datetime,
@Re nvarchar(50)--备注
AS
BEGIN
IF EXISTS (select * from tb_Stock1 where ProductID = @ProductID and DepotID=@DepotID )
BEGIN
update tb_Stock1 set Quantity = Quantity+@Quantity,TotalPrice=TotalPrice+@TotalPrice where ProductID=@ProductID and DepotID=@UDepotID
update tb_Stock1 set Quantity = Quantity-@Quantity,TotalPrice=TotalPrice-@TotalPrice where ProductID=@ProductID and DepotID=@DepotID
END
IF NOT EXISTS (select * from tb_Stock1 where ProductID = @ProductID and DepotID=@UDepotID)
BEGIN
INSERT INTO tb_Stock1--库存表
(
ProductID,
ProductName,
DepotID,
-- DepotName,
Spec,
Unit,
Quantity,
Price,
TotalPrice,
Stockdate
) values
(
@ProductID,--产品ID
@ProductName,--产品名称
@UDepotID,--存货仓库
-- @DepotName,
@Spec,--规格
@Unit,--单位
@Quantity,--数量
@Price,--单价
@TotalPrice,
@StockDate
)
END
INSERT INTO tb_ParticularDepot --出入库表
(
NumberId,
ProductID,
ProductName,
Spec,
Unit,
Quantity,
Price,
TotalPrice,
DepotID,
DepotName,
UDepotID,
State,
Operate,
StockDate,
Re
)
VALUES
(
@NumberId ,--单据凭证编号
@ProductID ,--产品ID
@ProductName ,--产品名称
@Spec ,--规格
@Unit ,--单位
@Quantity ,--数量
@Price ,--单价
@TotalPrice ,--总金额
@DepotID ,-- 接收仓库
@DepotName,
@UDepotID,
@State ,--仓库:为入库为出库
@Operate ,--操作员
@StockDate ,
@Re --备注
)
END