SQL Server 2008存储过程语法问题

时间:2016-01-21 16:25:37

标签: sql sql-server sql-server-2008 stored-procedures

这是我的第一个存储过程,我真的很感激一些帮助来制作它:)

我有一个去,但我得到以下错误出现在P.RL所在的地方。我尝试将P.RL重命名为Products.RL,结果相同。

Msg 4104, Level 16, State 1, Procedure UpdateInventoryForSCRAPorder, Line 19
The multi-part identifier "Products.HD" could not be bound.
Msg 4104, Level 16, State 1, Procedure UpdateInventoryForSCRAPorder, Line 19
The multi-part identifier "Q.QuantityOrdered" could not be bound.

也许这是一种更有效的方式来做我正在尝试的事情,或者只是需要进行语法调整。

此存储过程的目的是更新产品表中的库存级别。有两列用于保存库存数据。一列(RL)包含已通过质量检查并已被释放(因此为RL)以供订购的库存数量。

另一栏(HD)是针对因各种原因而持有的股票,但已确定它们可以包含在订单中。

因此,我在此尝试做的是确保在订购产品后立即更新库存。 if语句试图涵盖所有场景,例如某些已发布的产品以及一些已发布的产品。

例如,订购10个项目:如果RL列中有4个项目,请先将这些项目添加到订单中,然后从保留的列中取出剩余项目。

感谢所有帮助!

这是存储过程

CREATE PROCEDURE UpdateInventoryForSCRAPorder

@OrderID Int,
@LocalAmount Int

AS
BEGIN

    IF (Products.RL > 0 and Products.HD > 0) and (Q.QuantityOrdered > Products.RL)
      Begin

        set @LocalAmount = Q.QuantityOrdered - RL

        UPDATE P
        Set RL = 0,
            HD = HD - @LocalAmount,
            P.OutstandingOrders = P.OutstandingOrders + Q.QuantityOrdered
        From
            Products P  join    
            (Select ProductID, sum(Quantity) as QuantityOrdered from OrderDetails where OrderID = @OrderId Group By ProductId)  Q 
            on P.ID = Q.ProductID
      END

    ELSE IF (P.RL > 0 and P.HD > 0) and (Q.QuantityOrdered < P.RL)
        Begin
          UPDATE P
            Set RL = RL - Q.QuantityOrdered, 
            P.OutstandingOrders = P.OutstandingOrders + Q.QuantityOrdered
                From
                    Products P  join    
                    (Select ProductID, sum(Quantity) as QuantityOrdered from OrderDetails where OrderID = @OrderId Group By ProductId)  Q 
                    on P.ID = Q.ProductID
        END 
    ELSE IF (P.RL = 0 and P.HD > 0) 
        Begin
          UPDATE P
            Set HD = HD - Q.QuantityOrdered, 
            P.OutstandingOrders = P.OutstandingOrders + Q.QuantityOrdered
                From
                    Products P  join    
                    (Select ProductID, sum(Quantity) as QuantityOrdered from OrderDetails where OrderID = @OrderId Group By ProductId)  Q 
                    on P.ID = Q.ProductID
        END 
    ELSE IF (P.RL > 0 and P.HD = 0) 
        Begin
          UPDATE P
            Set RL = RL - Q.QuantityOrdered, 
            P.OutstandingOrders = P.OutstandingOrders + Q.QuantityOrdered
                From
                    Products P  join    
                    (Select ProductID, sum(Quantity) as QuantityOrdered from OrderDetails where OrderID = @OrderId Group By ProductId)  Q 
                    on P.ID = Q.ProductID
        END         

END

解决

经过一夜好眠和大量咖啡因后,我尝试了不同的方法并提出了一个不同的问题。

归功于Devart的最后一部分。见Single update statement method

显然我的要求归结为:

UPDATE Products
SET HD = HD + RL,
    RL = 0
FROM P
WHERE RL < 0
    AND ID IN (
            SELECT ProductID
            FROM dbo.OrderDetails
            WHERE OrderID = @OrderId
        )

3 个答案:

答案 0 :(得分:0)

我不知道你想要做什么,虽然它看起来比必要的复杂。但是,正文中的第一行有语法错误:

IF (Products.RL > 0 and Products.HD > 0) and (Q.QuantityOrdered > Products.RL)

什么是Products?什么是Q?限定列名属于SQL语句(SELECT / UPDATE / DELETE),而不属于控制流。

我不确定如何修复逻辑。将条件放在WHERE子句中可能是正确的解决方案。

答案 1 :(得分:0)

您不能直接在if语句中使用table_name.column_name,您可以使用select作为条件。例如:

If ( Select sum(RL) as RL from Product Group By ID)>0
begin
--put here your statement

end

或者你可以把变量:

declare @SumRL int
set @SumRL = (Select sum(RL) as RL from Product Group By ID)
if (@SumRL>0)
begin
--put here your statement
end

我真的不知道你要做什么。我不知道你使用的结构表,但我希望我的回答可以帮助你。

答案 2 :(得分:0)

只是为了好玩 -

CREATE PROCEDURE UpdateInventoryForSCRAPorder
(
    @OrderID INT,
    @LocalAmount INT
)
AS BEGIN

    UPDATE P
    SET RL -= CASE
                    WHEN (P.RL > 0 AND P.HD = 0) OR (P.RL > 0 AND P.HD > 0 AND Q.QuantityOrdered < P.RL)
                        THEN Q.QuantityOrdered
                    WHEN (P.RL > 0 AND P.HD > 0 AND Q.QuantityOrdered > P.RL)
                        THEN RL    
                    ELSE 0 END,
        HD -= CASE 
                    WHEN P.RL = 0 AND P.HD > 0
                        THEN Q.QuantityOrdered
                    WHEN (P.RL > 0 AND P.HD > 0 AND Q.QuantityOrdered > P.RL)
                        THEN @LocalAmount
                    ELSE 0
                END
        OutstandingOrders += Q.QuantityOrdered
    FROM Products P
    JOIN (
        SELECT ProductID,
                SUM(Quantity) AS QuantityOrdered
        FROM OrderDetails
        WHERE OrderID = @OrderID
        GROUP BY ProductId
    ) Q ON P.id = Q.ProductID
    WHERE (P.RL > 0 AND P.HD = 0)
        OR (P.RL = 0 AND P.HD > 0)
        OR (P.RL > 0 AND P.HD > 0 AND Q.QuantityOrdered < P.RL)
        OR (P.RL > 0 and P.HD > 0 AND Q.QuantityOrdered > P.RL)

END