chobo321321
Registered User.
- Local time
- Today, 11:56
- Joined
- Dec 19, 2004
- Messages
- 53
I'm trying to create a trigger that be able to update the UnitsOnHand (field) in the tblproduct table when an order is inserted, modified, or deleted in the tblOrder table. If an order is added it should take away from unitsonhand. If an order is modified it should add or take away depending on the adjustment to Qty in the order table. If the order is deleted then the quantity should be returned to UnitsOnHand.
I blow at triggers, and can't figure out to compare the qty in tblOrder table to the quantity in the insert or delete table, so I can figure out whether to add or subtract from the field. Any help is appreciated.
I blow at triggers, and can't figure out to compare the qty in tblOrder table to the quantity in the insert or delete table, so I can figure out whether to add or subtract from the field. Any help is appreciated.
Code:
CREATE TRIGGER UnitStock
ON tblOrder
AFTER INSERT, UPDATE, DELETE AS
UPDATE tblProducts
SET UnitsOnHand = UnitsOnHand - qty
FROM inserted
WHERE tblOrder.ProductID = inserted.ProductID