我的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

解决方案 »

  1.   

    因为@State=0不成立
    也就是你传的值不是0,
      

  2.   

    或是这两个UPdate无满足条件的数据
      

  3.   

    建议你看一本书《Visual Basic2005数据库入门经典》。
      

  4.   

    先select 一下,看看是不是有对应记录
      

  5.   

    可是我已经确定state=0的...我应该怎么做啊?
      

  6.   

    帮你测试了一下,在IF (@State=0)这句是没有问题的,以下是测试代码alter procedure SpExceedStock 
    @State int--仓库:为入库为出库 
    AS 
    BEGIN 
    SET NOCOUNT ON; 
    IF (@State=0)--出库
    begin 
    print 'OK'
    end
    else 
    begin
    print 'NG'
    end
    结果:
    /*
    OK
    */

    所以,问题应该在于你的两个update语句不满足条件
      

  7.   

    感谢各位的意见...我自己也是知道没错啦..可是下面这句话一直都不执行
    update tb_Stock1 set Quantity = Quantity-@Quantity,TotalPrice=TotalPrice-@TotalPrice where  ProductID=@ProductID and DepotID=@DepotID
    相对应的数据库当中都有满足记录的条件...
      

  8.   


    是不是有null
    Quantity = isnull(Quantity,0)-isnull(@Quantity,0)
      

  9.   

     感谢11楼的朋友
     可是Quantity 最小的值是0
     
     估计我另外没有想到
     
     感谢各位...
      

  10.   

    感谢各位前辈,我已将问题解决::::ALTER procedure [dbo].[SpExceedStock]
    @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