Could you please help me to finish my trigger. What i got so far:
CREATE TRIGGER [dbo].[atbl_Sales_OrdersLines_ITrigGG]
ON [dbo].[atbl_Sales_OrdersLines]
FOR INSERT
AS
BEGIN
DECLARE @ID INT = (SELECT ProductID
FROM INSERTED)
DECLARE @OrderedQ INT = (SELECT SUM(Amount)
FROM atbl_Sales_OrdersLines
WHERE ProductID = @ID)
DECLARE @CurrentQ INT = (SELECT Quantity
FROM atbl_Sales_Products
WHERE ProductID = @ID)
DECLARE @PossibleQ INT = (SELECT Amount
FROM INSERTED
WHERE ProductID = @ID)
IF (@CurrentQ - @OrderedQ >= @PossibleQ)
ELSE
END
I need to complete the code. Can not figure out how to do it. I need that if condition is met - trigger would allow the insert. If else, trigger would stop the insert/or rollback and prompt a message that quantity is not sufficient.
Also, will this code work if insert is multiple lines with different product ids?
Thanks.
@idcan only capture a single product id out of all the productIds ininsertedrows. So, everything after that is working with a subset of the truth.