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.
Related
Can I have a trigger to check any insert or update on whole database?
I tried this but it didn't work:
create trigger updateHistory after insert on database
Yaou need to elaborate the database into separate tables. And give the action. Eg:
CREATE TRIGGER updatehistory
BEFORE UPDATE ON `table,table2`
FOR EACH ROW
BEGIN
INSERT INTO
SET action = 'insert',
record = OLD.record,
changedat = NOW();
END
And as answered here, you cannot create both insert and update at the same trigger, but you can move the common code into a procedure and have them both call the procedure.
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'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
On MySQL 5.5, I am trying to write a trigger to do the following:
Checks a condition for the insert (is group_id between 8-20?)
If the condition is true, then update the user with the same user_id on another table, called phpbb_users.
The update on this other table is to set user_rank to NEW.group_id from the insert on the table that has the trigger.
Else, do nothing.
Not experienced at all, and stuck. It's not working when I run the query. Here is what I've got so far.
DELIMITER $$
CREATE OR REPLACE TRIGGER upd_rank AFTER INSERT ON `phpbb_user_group`
FOR EACH ROW BEGIN
IF (NEW.group_id BETWEEN 8 AND 20) THEN
UPDATE phpbb_users
SET user_rank=NEW.group_id
WHERE user_id=NEW.user_id;
ELSE
UPDATE phpbb_users
SET user_rank='0'
WHERE user_id=NEW.user_id;
END IF;
END$$;
DELIMITER;
(Note, the "else" clause is unnecessary really - happy for any suggestions for improvement of this code!)
if you only want to get rid of the else try
CREATE TRIGGER upd_rank AFTER INSERT ON `phpbb_user_group`
FOR EACH ROW
UPDATE phpbb_users
SET user_rank=IF(NEW.group_id BETWEEN 8 AND 20,NEW.group_id,"0")
WHERE user_id=NEW.user_id;
Using MySQL 5.1.x
Trying to add a trigger to a table:
DELIMITER $$
CREATE TRIGGER group AFTER INSERT ON dataTable
FOR EACH ROW BEGIN
UPDATE dataTable SET groupName = mid(longName,1,4) WHERE groupNAME IS NULL;
END$$
When I insert a record there is no update made. Is there a syntax error? Or can I not run the Update query on the after insert event?
UPDATE: There are 2 triggers on this table (a AFTER INSERT and BEFORE UPDATE).
In a MySQL trigger, you cannot invoke DML on the table that fires the trigger.