I am trying to create a Trigger for a table that UDATES. I thought I could do this with multiple Triggers but I guess MySQL does not support multiple triggers on one table for the same type. So I a forced to include this into one trigger.
Basically I have trying to insert a new record into a different table based on what is updated in the current table.
Here is what I have so far and it works:
BEGIN
-- Definition start
INSERT INTO prices (stationID, price, type,
prevPrice, prevDate, dateCreated, apiKey, uid)
VALUES(NEW.stationID, NEW.regPrice, 'reg', OLD.regPrice,
OLD.regDate, NEW.regDate, NEW.lastApiUsed, NEW.lastUpdatedBy);
-- Definition end
END
What I need to happen is the type may change based on what type the user gives. Since the user may only update one type at a given time. I was thinking about an IF IF THEN statement but I wasn’t sure how to get it to work. I was thinking I could use a String value of the column name to use in the IF statement but I an not sure if that is possible.
Something like this:
BEGIN
-- Definition start
IF(type == 'reg') THEN
INSERT INTO prices (stationID, price, type,
prevPrice, prevDate, dateCreated, apiKey, uid)
VALUES(NEW.stationID, NEW.regPrice, 'reg', OLD.regPrice,
OLD.regDate, NEW.regDate, NEW.lastApiUsed, NEW.lastUpdatedBy);
END IF;
-- Definition end
END
I was able to use
IF THEN ELSEto complete the transaction. Then I tested the condition for aNULLvalue. If it isTRUEI then ran my code. Now for some reason it only does oneINSERTcommand even though theUPDATEis for more than one. Any thoughts on this?