SQL Server存储过程中的条件语句

时间:2012-08-21 19:23:07

标签: sql sql-server stored-procedures conditional

我正在编写一个存储过程,例如在更新语句中但是ma混淆了如何应用这种类型的东西?

我的存储过程是:

ALTER PROCEDURE [dbo].[Payment_SP]
    @reciptId varchar(50)=null,
    @balPay float=null,
    @payDone float=null,
    @payDate datetime=null,
    @fullfillId_FK varchar(50)=null,
    @clintId_FK int=null,
    @status varchar(50)=null,
    @operation int,
    @fullfillId varchar(50)=null
AS
BEGIN   
    if @operation = 3
    BEGIN
        UPDATE Recipt 
        SET balPay = @balPay 
        WHERE reciptId = @reciptId

        if @@rowcount = 0
            insert into Recipt(reciptId, balPay, payDone, payDate, fullfillId_FK, clintId_FK)
            values(@reciptId, @balPay, @payDone, @payDate, @fullfillId_FK, @clintId_FK)
        update Item_Full 
        set totCost = (select balPay from Recipt) 
        where fullfillId = @reciptId

        if @balPay='0'
        BEGIN
            update Item_Order 
            set status = 'CLOSE' 
            where status = 'FULLFILL' 
               or status = 'RUNNING' 
              and orderId = @reciptId
        END
        ELSE
        BEGIN
            update Item_Order 
            set status = 'RUNING' 
            where status = 'FULLFILL' 
              and orderId = @reciptId
        END
    END
END

这里我使用了大量的表格和列。但是如果余额支付(@balPay = 0)我想在Recipt表格中做什么,那么状态=关闭,否则如果有任何余额> 0然后status = RUNING

但每次付款完成后,我的状态为RUNING。这意味着只有部分条件语句才会执行,而不是部分

if语句

中的条件应该是什么

谢谢

2 个答案:

答案 0 :(得分:0)

@balPay永远不会从null更新,所以你的else语句总是被执行。

答案 1 :(得分:0)

好吧,伙计们,我做了一个愚蠢的拼写错误,我在其他RUNING中改变状态,如果条件正在寻找RUNNING ...抱歉,谢谢