简体   繁体   English

T-SQL中的嵌套If条件

[英]Nested If Conditions in T-SQL

Hello i have the following store procedure: 您好我有以下存储过程:

    CREATE PROCEDURE update_stock_level
(
    @ItemID int,
    @Quantity int = 1,
    @Return int = 0
)
AS

    IF @Return = 0
        UPDATE  Stock
        SET     Quantity    = (Quantity - @Quantity)
        WHERE   ItemID      =   @ItemID

    ELSE 
        UPDATE  Stock
        SET     Quantity    = (Quantity + @Quantity)
        WHERE   ItemID      =   @ItemID

This stored procedure is for A POS application, what this procedure does(should do) is update(increase or decrease) the stock level based on the item id and the item's quantity. 此存储过程适用于POS应用程序,该过程要做(应该做)是根据物料ID和物料数量更新(增加或减少)库存水平。

ItemID -> ID of item selected from front end. ItemID->从前端选择的项目的ID。 Quantity -> How many items Return -> If return is '0' that means it is false, meaning this is not a return sale if it is '1' it means an item is being returned in which case the stock should increase by the quantity returned. 数量->多少个商品退货->如果退货为'0'则表示是假的,这意味着如果退货为'1'则不表示退货,这表示商品正在退货,在这种情况下库存应增加退货数量。 If nothing is passed to the qantity parameter then the default is 1. 如果未将任何内容传递给qantity参数,则默认值为1。

My issue now is that the stock level get's reduced fine but it does not increase when a return is made. 我现在的问题是,库存水平降低了罚款,但退货时却没有增加。

Leave just ELSE part: 仅保留ELSE部分:

IF @Return = 0
    UPDATE  Stock
    SET     Quantity    = (Quantity - @Quantity)
    WHERE   ItemID      =   @ItemID
ELSE 
    UPDATE  Stock
    SET     Quantity    = (Quantity + @Quantity)
    WHERE   ItemID      =   @ItemID

Alternatively you can do it in a single statemnet: 另外,您也可以在单个statemnet中完成此操作:

UPDATE  Stock
SET     Quantity    = Quantity - @Quantity * POWER(-1,@Return)
WHERE   ItemID      = @ItemID

If @Return=0 then power(-1,0) = 1 and you decrease stock. 如果@ Return = 0,则功率(-1,0)= 1,您将减少库存。

If @Return=1 then power(-1,1) = -1 and you increase stock. 如果@ Return = 1,则功率(-1,1)= -1,您增加了库存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM