I get the following error: "Can't update table 'Product' in stored function/trigger because it is already used by statement which invoked this stored function/trigger."
CREATE TRIGGER `SalesDB`.`updateSum` AFTER UPDATE ON SalesDB.Product FOR EACH ROW
BEGIN
DECLARE totalStock INT;
SELECT SUM(stock)
INTO totalStock
FROM Product
WHERE Product.prodID= NEW.prodID;
UPDATE Product SET StockSum = totalStock
WHERE prodID = NEW.prodID;
END;
I understand that there is a recursive issue, but how do I do something like this? Is there a way to use triggers to do this kind of thing? Should this be done in a Stored Procedure instead, and how do I run the stroed procedure on a Update Product event?
I need to update the stock of a product and then go thru all rows that have the same prodID calculate the SUM and Update the SumTotal field of all the rows.