Click here to Skip to main content
15,888,097 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So, ive got a relational database, and im trying to create triggers that basically update other fields..

To provide more information. I have a big database, but shall only provide q couple of tables as the other fields are just employee tables etc and are unnecessary.

The tables ill include are Order and Stock..

Order (OrderID, ProductID, ItemQuantity)
Stock (StockID, ProductID, StockQuantity)

Try not to focus on nit picking my database, i just want to create a working functioned trigger.

I basically want to create a trigger than updates the StockQuantity from the Stock table by taking the ItemQuantity in the order table away from StockQuantity. How do i go about doing this? It will take away the quantity from the stock level that will be based on the productID.. So essentially something like...

Trigger = StockQuantity = StockQuantity - ItemQuantity where ProductID in Stock is same as ProductID in Order.
Posted
Comments
PIEBALDconsult 13-Feb-15 15:37pm    
Triggers are generally not a good idea.

1 solution

You just need to do a join in your trigger.

Something like this should work:
SQL
UPDATE Stock
SET StockQuantity = StockQuantity - o.ItemQuantity
FROM Stock s
INNER JOIN inserted i ON s.ProductID = i.ProductID
INNER JOIN Order o ON s.ProductID = o.ProductID


But my guess is you have lots of orders so you may have to do something like
SQL
UPDATE Stock
SET StockQuantity = StockQuantity - SUM(o.ItemQuantity)
FROM Stock s
INNER JOIN inserted i ON s.ProductID = i.ProductID
INNER JOIN Order o ON s.ProductID = o.ProductID


Inner Join to the inserted table in your trigger so that only those records that are being changed will be updated. Or, remove inserted to do the entire table.
 
Share this answer
 
v2
Comments
PIEBALDconsult 13-Feb-15 15:38pm    
If it's in a trigger, shouldn't it JOIN to the INSERTED table ?
ZurdoDev 13-Feb-15 15:40pm    
Yes, sorry, got carried away and forgot this was a trigger. Was just giving user part of everything.

I'll update it. Thanks.
[no name] 13-Feb-15 15:54pm    
This is my trigger code altogether.

CREATE OR REPLACE TRIGGER stock_trigger on [dbo].[Order]
FOR INSERT AS
UPDATE Stock
SET StockQuantity = StockQuantity - o.ItemQuantity
FROM Stock s
INNER JOIN inserted i ON s.ProductID = i.ProductID
INNER JOIN [Order] o ON s.ProductID = o.ProductID
GO
[no name] 13-Feb-15 15:51pm    
It seems to work, but it seems to work wrong. Basically each time a record is inserted on the Orders table, i want the quantity to be taken away from the StockQuantity in the Stock Table. It seems to take it away, but it seems to be taking away different numbers each time.
ZurdoDev 13-Feb-15 15:59pm    
Ya, it's going to keep decrementing unless you somehow set a flag as to which ones have already been decremented.

Perhaps you really want a trigger on Order so that when an order is created/modified, then reduce quantity.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900