how to get value from select query in trigger in mysql5? - mysql

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

For an INSERT Trigger query you would use the object NEW
For an UPDATE Trigger query you would use the object OLD and NEW
For a DELETE Trigger query you would use the object OLD
Example 1 : iF you ran INSERT INTO mytable (num) VALUES (10);
In the INSERT trigger, you reference the 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 ;

Related

Create a trigger that finds string value and copies ID to insert to new table

I am trying to write an MYSQL statement that will copy the patient_id where they have a value of "Dose: Second" and paste it to my table vaccinatedpatients.
Also, I wanted to implement this within a trigger to automatically update itself after each new record on the table.
Any help with this will be great!
CREATE DEFINER=`root`#`localhost` TRIGGER `patient_AFTER_INSERT` AFTER INSERT ON `patient` FOR EACH ROW BEGIN
SELECT #variable = patient_id from patient where vaccine_Dose = 'Dose: Second';
INSERT INTO vaccinatedpatients VALUES(variable);
END
You can use an IF cLause, to determines if the patient gets the second dose
DELIMITER &&
CREATE DEFINER=`root`#`localhost` TRIGGER `patient_AFTER_INSERT` AFTER INSERT ON `patient` FOR EACH ROW
BEGIN
IF NEW.vaccine_Dose = 'Dose: Second' THEN
INSERT INTO vaccinatedpatients VALUES (NEW.patinet_id);
END IF;
END&&
DELIMITER ;
The DELIMITER may not be needed.

I can't update my table with trigger in mysql

I'm trying to fill the empty field in the table after I update that table but I am get this error :
#1442 - Can't update table 'services_offer' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
How can I fix it?
my trigger's query:
DELIMITER $$
Create trigger OfferSum
after UPDATE
on Services_Offer
FOR EACH ROW
BEGIN
UPDATE Services_Offer
SET NEW.final_price =
(SELECT service_price from Services_Level
where Services_Level.level_code= Services_Offer.level_code)*Services_Offer.service_size;
END$$
I think you want a before update trigger:
DELIMITER $$
CREATE TRIGGER OfferSum BEFORE UPDATE ON Services_Offer
FOR EACH ROW
BEGIN
SELECT NEW.final_price := sl.service_price * NEW.service_size
FROM Services_Level sl
WHERE sl.level_code = NEW.level_code;
END$$
Why do you need a trigger for this? It looks like the query above will result in and endless loop. Every time you update a row, it calls an update trigger which actually updates the table and thus triggers the trigger again and again and again until something crashes.
Instead, why don't you calculate and update the column withing the initial update. The query would look like this:
UPDATE Services_Offer
SET
column1 = value1,
column2 = value2,
final_price =
(
SELECT service_price
FROM Services_Level
WHERE Services_Level.level_code = Services_Offer.level_code
) * service_size
WHERE id = XXX
where column1 = value1, column2 = value2 comes from your initial update query.

sum trigger on insert Mysql

I have a table called sum_test with id,a,b,c fields
I need a trigger that calculates the sum of a+b on c but Im new to triggers.
How can I do that on Mysql?
CREATE TRIGGER `example` AFTER INSERT ON `sum_test`
FOR EACH ROW BEGIN
UPDATE sum_test
SET c= (a+b)
WHERE id = id
END
If we are wanting to set column c on the row we are inserting, then we can do like this, in a BEFORE insert trigger:
DELIMITER $$
CREATE TRIGGER `sum_test_bi` BEFORE INSERT ON `sum_test`
FOR EACH ROW
BEGIN
SET NEW.c = NEW.a + NEW.b ;
END$$
DELIMITER ;
I don't understand the reference to table suma. What does that table have to do with anything? We note that the condition id = id is going to evaluate to TRUE for every row in the table where id IS NOT NULL.

mySQL trigger not firing

I have a table with 4 columns [year, v_num, t_num, percentage], I'm trying to populate percentage with v_num/t_num*100. The data is already in the table but the percentage column is NULL. I created an AFTER INSERT trigger to populate the column but it's not working. Does anyone have any suggestions?
TRIGGER
DELIMITER |
CREATE DEFINER = 'XXX' TRIGGER percentAfterUp
AFTER INSERT
ON `table`
FOR EACH ROW
BEGIN
UPDATE `table` SET percentage = v_num/t_num*100;
END|
DELIMITER ;
If you want to set a value in a row, do it with a before insert trigger, not an after insert trigger:
DELIMITER |
CREATE DEFINER = 'XXX' TRIGGER percentAfterUp
BEFORE INSERT ON `table` FOR EACH ROW
BEGIN
SET new.percentage = (new.v_num / new.t_num) * 100;
END|
DELIMITER ;
If you actually want to update all the rows, don't use a trigger, just an update statement:
UPDATE `table`
SET percentage = (v_num/t_num)*100;

Mysql : Trigger after insert copy content into another table and delete row from triggered table

Here I want to copy the contents of X table into Y table and on the same lines I wanted to delete the row from X table.I tried it in following way but it is not working.
Drop trigger if exists myTrigger;
delimiter |
create trigger myTrigger
after insert on X
for each row
BEGIN
IF STRCMP(NEW.SysLogTag,"kernel:") = 0 THEN
INSERT INTO Y(logtime,moduleid,severity,messageid,message) values(NULL,1,1,100,NEW.Message);
ELSEIF NEW.SysLogTag like 'ntpd[%]:' THEN
INSERT INTO Y(logtime,moduleid,severity,messageid,message) values(NULL,6,1,100,NEW.Message);
ELSE
INSERT INTO Y(logtime,moduleid,severity,messageid,message) values(NULL,4,1,100,NEW.Message);
END IF;
delete from X where ID=NEW.ID; //Not working.
END;|
delimiter ;
From http://dev.mysql.com/doc/refman/5.1/en/create-trigger.html#c8808 :
My solution was to create a unique/primary key on the table I want to insert into (in this case on S_ID), then if my sanity check fails I change the s_id to 0. This method will only ever create one duff record with an s_id of 0. Obviously you need to INSERT ignore!
CREATE TRIGGER sanityCheck
BEFORE INSERT ON my_table
FOR EACH ROW
BEGIN
IF something THEN
SET NEW.S_ID = 0 ;
END IF;
END;