I have to table named 'stuff' and 'given'. I wanted to add one more columnt to 'stuff' and after ALTER TABLE i used UPDATE. However this makes difference for only that moment. I mean, m new column will give the amount of stock after each given amount. 'stuff' has 'totalAmount' and given has 'amount'.
I used:
ALTER TABLE stuff ADD stock MEDIUMUNINT UNSIGNED;
UPDATE stuff SET stuff = totalAmount - (SELECT SUM(given.amount) FROM given WHERE id = given.productId);
This works for only that UPDATE. How can i make these two table so synchronized after each given amount, the stock will be affected either?
Thanks in advance :)
Create an trigger on INSERT for the table "given" which will update the "stuff" in table "stuff"
CREATE TRIGGER [TRIGGER_ALTER_STUFF] ON [dbo].[given]
FOR INSERT, UPDATE
AS
BEGIN
UPDATE stuff SET stuff = totalAmount - (SELECT SUM(given.amount) FROM given WHERE id = given.productId);
END
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.