How to get value from select query in trigger in mysql5?

How to get a value from a select query in a trigger and insert that value into a table?

+6
sql mysql triggers
source share
1 answer

To request an INSERT trigger, you must use the NEW object
For an UPDATE Trigger query, you must use the OLD object and NEW
For a DELETE Trigger query, you must use an OLD object

Example 1: iF you ran INSERT INTO mytable (num) VALUES (10);
In an INSERT trigger, you refer to a column as NEW.num (10);

Example 2: iF you ran UPDATE mytable SET num = 41 WHERE num = 10;
In the UPDATE trigger, you reference OLD.num (10) and NEW.num (41)

Example 3: iF you ran DELETE mytable num = 104;
In the DELETE trigger, you reference OLD.num (104)

Use something like this:

DELIMITER $$ create trigger my_trigger AFTER UPDATE on my_update_table for each row begin DECLARE P1,P2 VARCHAR(50); SELECT PRICENAME INTO P1 FROM PRICEIES WHERE PRICEID=OLD.PRICEID; SELECT PRICENAME INTO P2 FROM PRICEIES WHERE PRICEID=NEW.PRICEID; INSERT INTO AUDITLOG(OLDVALUE, NEWVALUE) VALUES (P1,P2); end $$ DELIMITER ; 
+14
source share

All Articles