這是我的第一個存儲過程,我真的很感激一些幫助起草吧:)解決 - 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
)
感謝所有誰貢獻,我有很多學習!
'IF(Products.RL> 0和Products.HD> 0)'這裏是'SELECT'? – lad2025
嗨,即時嘗試更新表值,而不是選擇記錄。歡呼聲 – Nick
'SQL存儲過程'不像OO世界像JAVA/C#。你不能引用'table_name.column_name'沒有任何其他子句。 – lad2025