2016-01-21 58 views
0

這是我的第一個存儲過程,我真的很感激一些幫助起草吧:)解決 - SQL Server 2008中存儲過程語法問題

我有去,但即時得到它的地方出現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 
     ) 

感謝所有誰貢獻,我有很多學習!

+2

'IF(Products.RL> 0和Products.HD> 0)'這裏是'SELECT'? – lad2025

+0

嗨,即時嘗試更新表值,而不是選擇記錄。歡呼聲 – Nick

+2

'SQL存儲過程'不像OO世界像JAVA/C#。你不能引用'table_name.column_name'沒有任何其他子句。 – lad2025

回答

0

我不知道你在做什麼,雖然看起來比需要更復雜。然而,你對第一行語法錯誤在身:

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

什麼是Products?什麼是Q?合格列名屬於SQL語句(SELECT/UPDATE/DELETE),不在控制流中。

我不知道如何修復邏輯。將條件放在WHERE條款中可能是正確的解決方案。

+0

謝謝你。我試圖做的是,對於給定的訂單,說它包含orderdetails表中的四個項目,對於每個項目,我想在產品表中找到該產品,然後減去訂購的數量。可訂購的數量分成兩列,所以我需要先從RL列中減去,如果有餘數,則從HD列中取出。這是否回答你的問題? – Nick

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 

我真的不知道你正在嘗試做的。我不知道你使用的結構表,但我希望我的答案可以幫助你。

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