I'm trying to set up a trigger which will update same row by inserting some additional data from another table. Field receive.iccid is blank, and I want it to be updated on every insert. However this trigger doesn't work
delimiter //
CREATE TRIGGER ins_iccid
AFTER INSERT ON receive
FOR EACH ROW
BEGIN
UPDATE receive SET NEW.iccid = (SELECT goip.iccid FROM goip WHERE NEW.goipname=goip.name);
END//
delimiter ;
turns out that I need to use 'BEFORE INSERT' to achieve what i was trying to
CREATE TRIGGER `ins_iccid` BEFORE INSERT ON `receive`
FOR EACH ROW
BEGIN
SET NEW.iccid = (SELECT goip.iccid FROM goip WHERE NEW.goipname=goip.name);
END
Related
I'm trying to insert a row into a table after updating a row in this same table. I was trying to do this using a trigger. But I found that it's not possible to do that in this method.
What i'm trying to achieve is, I have this
tbl_eq_maintenance(eq_no,eq_name,last_rep_date,next_rep_date,status)
Values(EQ-30,treadmill,08-10-2018,18-10-2018,0).
I want to update this row's status into "1" and after that insert a new row into the same table as follows.
(EQ-30,treadmill,18-10-2018,28-10-2018,0).
How can i do this with a trigger or if can't is there a workaround for this.
The trigger I wrote as below.
DELIMITER $$
CREATE TRIGGER add_maintenance
AFTER UPDATE on tbl_eq_maintenance
FOR EACH ROW
BEGIN
DECLARE next_rep_date DATE;
DECLARE new_m_cycle INT(5);
SELECT m_cycle INTO new_m_cycle FROM tbl_equipments WHERE eq_no = NEW.eq_no;
SET next_rep_date = DATE_ADD(CURDATE(), INTERVAL new_m_cycle DAY);
INSERT INTO tbl_eq_maintenance (eq_no,eq_name,last_rep_date,next_rep_date)
Values (NEW.eq_no,NEW.eq_name,CURDATE(),next_rep_date);
END$$
DELIMITER ;
When you update the table, this trigger will execute and insert an new record in the table.
Refer to this link
I have this code for the trigger:
CREATE TRIGGER `insert_after` AFTER INSERT ON `hyk50_0001`
FOR EACH ROW BEGIN
INSERT INTO hyk50_0001_copy(Fecha)
SELECT Fecha FROM hyk50_0001
END;
but doesn't work, it says a syntax error but I didn't see it
I'm using Navicat. And hyk50_0001_copy it's a identical copy of hyk50_0001.
The target is take the new row of hyk50_0001 and INSERT in hyk50_0001_copy
I want put all the database, but if it doesn't work with only a value I can't progress.
I don't see a syntax error, but I assume you intend:
DELIMITER $$
CREATE TRIGGER `insert_after` AFTER INSERT ON `hyk50_0001`
FOR EACH ROW
BEGIN
INSERT INTO hyk50_0001_copy(Fecha)
VALUES (new.Fecha)
END;$$
DELIMITER ;
That is, you probably want to insert only the row that was just created.
I have a table with 2 columns named "Table1":
(Column 1 named "Col1" with the values: A,B,C,D,E,F)
(Column 2 named "Col2" with the values: 12,15,2,5,200,1).
I would like get all the values from column 2 to change to the value 1 if their value is lower than 100, so that column 2 will eventually look like this:
(Column 2 named "Col2" with the values: 1,1,1,1,200,1).
I tried to create a trigger:
delimiter //
CREATE TRIGGER Table1upd BEFORE UPDATE ON Table1
FOR EACH ROW
BEGIN
IF NEW.Col2<100 THEN
SET NEW.Col2=1;
END IF;
END;//
delimiter ;
When creating the trigger in MySQL workbench it says that the trigger was added but that 0 row(s) affected.
I assume my problem is with the BEFORE UPDATE choice, because the trigger does work when I update a value in the table, but I don't know what to change it to so that the trigger will also initially execute automatically when I create it.
Thank you in advance for any help,
D
The answer is simple: a BEFORE UPDATE trigger only triggers on UPDATE, so you need a second trigger BEFORE INSERT to cover both use cases: update and insert:
delimiter //
CREATE TRIGGER Table1insert BEFORE INSERT ON Table1
FOR EACH ROW
BEGIN
IF NEW.Col2<100 THEN
SET NEW.Col2=1;
END IF;
END;//
delimiter ;
MySql trigger is very interesting, but very tricky.
I have some problem, I want to run a trigger once after insert on .
I want to run my trigger once after rows inserted, is there anything like for each table``???
how to make this trigger run only once, but not for each row created.
CREATE
DEFINER=`root`#`localhost`
TRIGGER `mydb`.`leave_taken_trigger`
AFTER INSERT ON `mydb`.`leave`
FOR EACH ROW
BEGIN
set #lt:= (SELECT count(*) FROM `leave` where (staff_leave_application_staff_id_staff = new.staff_leave_application_staff_id_staff and leave_type_id_leave_type = new.leave_type_id_leave_type) and active = 1 );
INSERT INTO `leave_taken`(leave_type_id_leave_type, staff_id_staff, taken_days, updated)
VALUES (new.leave_type_id_leave_type, new.staff_leave_application_staff_id_staff, IFNULL(#lt,0), CURDATE())
ON DUPLICATE KEY UPDATE taken_days=IFNULL(#lt,0), updated = CURDATE();
END$$
I think you can't achieve your requirement with trigger.
Your trigger will run in every row created.
I suggest you to using stored procedure from your code after the INSERT has successfully completed.
Hi guys I intend to create a Trigger for MySQL table so that after Values are Inserted into the table, the Column exp_sales is set to qnty_received value *selling_price but I am getting this error:
ERROR 1193 (HY000): Unknown system variable 'exp_sales'
This is My Query:
delimiter $$
Create trigger tsales after insert on Store_info_table
for each row
set exp_sales = qnty_received * selling_price;
END$$
What is the problem and what is the best way to create the trigger so that after teh Quantity and selling Price is Inserted, the field for exp_sales(Total) is Updated with the right value?
If I unserstand corrctly exp_sales is a column name you want to update:
delimiter $$
Create trigger tsales after insert on Store_info_table
for each row
update Store_info_table
set exp_sales = NEW.qnty_received * NEW.selling_price
where id = NEW.id;
END$$
better approach would be to use INSERT INTO ON DUPLICATE KEY UPDATE
INSERT INTO table_name(...)VALUES(...)
ON DUPLICATE KEY SET exp_sales = qnty_received * selling_price;
If I understood you right you want to update a field of your record after insert. Try
delimiter $$
Create trigger tsales after insert on Store_info_table
for each row
begin
UPDATE Store_info_table
SET exp_sales = NEW.qnty_received * NEW.selling_price
WHERE id = NEW.id;
END;
$$
Haven't you forgotten about the NEW keyword?
Try this one -
SET NEW.exp_sales = NEW.qnty_received * NEW.selling_price;
But, I think you should not do this, because you always can calculate 'total' value in SELECT query.
Try the following code, it works for me
create trigger tsales
after insert
on
Store_info_table
for each row
set new.exp_sales = new.qnty_received * new.selling_price;
but since you want the exp_sales calculated automatically, why would you not use before instead of after?