I have two tables (stock and productsLot) and I need a trigger that calculates the total quantity of a product(in stock table) by summing the lot quantities(from productsLot).
I don't know much about triggers but my problem is similar to this one Calculate column value from another column in another table. Here's my case.
CREATE TABLE Stock (productId INTEGER,totalQuantity INTEGER, price NUMERIC(30,2))
PRIMARY KEY (productId);
CREATE TABLE productsLot (productId INTEGER CONSTRAINT fk_stock_productsLot REFERENCES ON Stock(productId) , lotNumber VARCHAR, lotQuantity INTEGER, expirationDate DATE, PRIMARY KEY (productId, lotNumber));
CREATE TRIGGER update_quantity_stock
BEFORE INSERT ON stock SET NEW.totalQuantity =
(
SELECT sum(lotQuantity)
FROM productsLot
WHERE productId = NEW.productId
LIMIT 1
);
I'm getting this error:
syntax error at or near "SET"