forked from TrainingByPackt/SQL-for-Data-Analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2605664
commit e8fe980
Showing
1 changed file
with
15 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
DROP FUNCTION update_stock; | ||
CREATE FUNCTION update_stock() RETURNS TRIGGER AS $stock_trigger$ | ||
DECLARE | ||
stock_qty integer; | ||
BEGIN | ||
stock_qty := get_stock(NEW.product_code) - NEW.qty; | ||
UPDATE products SET stock = stock_qty WHERE product_code=NEW.product_code; | ||
RETURN NEW; | ||
END; $stock_trigger$ | ||
LANGUAGE PLPGSQL; | ||
|
||
CREATE TRIGGER update_trigger | ||
AFTER INSERT ON order_info | ||
FOR EACH ROW | ||
EXECUTE PROCEDURE update_stock(); |