I'm having trouble understanding how triggers execute in MySQL and I'm banging my head on the wall, because I can't seem to find out why it doesn't work.
I have the following trigger
CREATE TRIGGER Insert_Products BEFORE INSERT ON `Products`
FOR EACH ROW BEGIN
DECLARE x_ProductID INT;
SET x_ProductID = NEW.`ProductID`;
SET NEW.`PriceExVAT` = (
SELECT
ROUND(p.`Price` * 100 / (100 + v.`VATPercentage`), 2) as priceexvat
FROM
`Products` p
LEFT JOIN
`VAT` v ON p.`VATID` = v.`VATID`
WHERE p.`ProductID` = x_ProductID); -- also tried inserting NEW.`ProductID` directly into this line
END $$
However it populates my rows with null instead of the correct values. HOWEVER, putting it in a select query results the correct values. IE:
SELECT
ROUND(p.`Price` * 100 / (100 + v.`VATPercentage`), 2) as x_value
FROM
`Products` p
LEFT JOIN
`VAT` v ON p.`VATID` = v.`VATID`
WHERE p.`ProductID` = 1;
I tried putting it in an AFTER INSERT trigger, but that resulted in a different error. What am I not seeing, how should I fix this?
`Products`.`ProductID`isINT(11) UNSIGNED NN AIso the value is not inserted. How would I still use this value in aBEFORE INSERT ONtrigger?