MySQL TRIGGER to INSERT BEFORE INSERT using SELECT, IF, etc - mysql

I am trying to write a trigger that combines an insert & select, I've found numerous topics online, but, none seem to relate to my exact problem, maybe I am missing something with my structure?
The aim of this is that on the event of a cancellation in our audit log, then I define a cancellation reason based on a series of business logic in another table, this logic is drawn together in a SELECT using CASE & subqueries.
I want to expand the following trigger that currently works and replace the SET cancellation_point='test' element with the SELECT query I just mentioned.
DELIMITER $$
CREATE
TRIGGER `cancellation_stage` BEFORE INSERT ON `log`
FOR EACH ROW
BEGIN
IF (NEW.status='cancel' AND NEW.type='0') THEN
INSERT INTO cancellation_stage
SET
id=NEW.id,
property_id=NEW.entity_id,
cancellation_date=NOW(),
cancellation_point='test';
END IF;
END;
$$
DELIMITER ;
I did try to construct this myself using various guidance from here, but, its just not working. I got this code to physically save as a trigger, but, it did not populate the data in the database (I have replaced my SELECT with a basic query example below):
DELIMITER $$
CREATE
TRIGGER `cancellation_stage` BEFORE INSERT ON `log`
FOR EACH ROW
BEGIN
DECLARE cancellation_point VARCHAR(255);
SET cancellation_point = ( SELECT * FROM x);
IF (NEW.transition='cancel' AND NEW.entity_type='property') THEN
INSERT INTO cancellation_stage
SET
id=NEW.id,
property_id=NEW.entity_id,
cancellation_date=NOW(),
cancellation_point=NEW.cancellation_point;
END IF;
END;
$$
DELIMITER ;
Any help would be greatly appreciated :)

Related

How to create 'before insert' trigger that looks at multiple tables

I am struggling to understand the format to create an 'insert before' trigger that must meet conditions of other tables.
For example, if I have a table named 'borrowed' that I would like to insert values into but must meet conditions from another table called 'member'. The condition is that MemberStatus = 'regular'. If this condition passes, I will be able to run a 'insert into borrowed' but if the conditions aren't met then it will not be able to pass.
This is how I previously understood the concept but after hours of reading i've become confused. Any help would be appreciated.
delimiter //
drop trigger if exists trigger1
//
create trigger trigger1
before insert on borrowed
for each row
begin
if...
end if;
end;
//

MYSQL fires the trigger only when no value manually inserted - not working

Hi I just need help as I am just new to mysql trigger.
I'm trying to create a trigger that if the project id is NULL then it will set it to new value but if someone has manually insert a value then it won't do anything.
I performed this but it ain't working on mysql.
CREATE TRIGGER custom_autonums_pdl BEFORE INSERT ON project_details_logs
FOR each ROW
WHEN (new.projectid IS NULL)
BEGIN
SET new.projectid = getNextCustomSeqPl(year(now()),year(now()));
END//
delimiter ;
If someone can direct me to correct this, appreciate it.
Thank you.
I haven't seen a MariaDb/MySQL trigger that uses the WHEN syntax available in other major RDBMS - you'd need to run the trigger for every insert and conditionally act on the id:
DELIMITER |
CREATE TRIGGER custom_autonums_pdl
BEFORE INSERT ON project_details_logs
FOR each ROW
BEGIN
IF new.projectid IS NULL THEN
SET new.projectid = getNextCustomSeqPl(year(now()),year(now()));
END IF;
END|
DELIMITER ;

MySQL error code 1235

In MySQL I tried to define a trigger like this:
DELIMITER $$
CREATE TRIGGER vipInvite
AFTER INSERT ON meetings
FOR EACH ROW
BEGIN
IF(NOT EXISTS (SELECT * FROM participants
WHERE meetid = NEW.meetid AND pid ='vip'))
THEN
IF(EXISTS(SELECT * FROM meetings WHERE meetid = NEW.meetid AND slot > 16))
THEN
INSERT INTO participants(meetid, pid)
VALUES (NEW.meetid,(SELECT userid
FROM people WHERE people.group = 'tap' GROUP BY invite));
END IF;
END IF;
END $$
DELIMITER ;
Produces this error:
This version of MySQL doesn't yet support 'multiple triggers with the same action time and event for one table.
Is there a way to work around this so I can define multiple triggers?
This error means you already have an AFTER INSERT trigger on meetings table.
If it is the same trigger (meaning vipInvite) that you created earlier and now you want to replace it then you need to drop it first
DROP TRIGGER vipInvite;
DELIMITER $$
CREATE TRIGGER vipInvite
...
END$$
DELIMITER ;
Now if you have some other trigger you have to merge code from both triggers into one, then drop existing trigger, and then create a new one.
To show the list of existing triggers use SHOW TRIGGERS.
SHOW TRIGGERS WHERE `table` = 'meetings';
How to reproduce this error in MySQL:
ERROR 1235 (42000): This version of MySQL doesn't yet support 'multiple
triggers with the same action time and event for one table'
Run the following queries:
DELIMITER //
CREATE TRIGGER mytrigger1 AFTER INSERT ON mytable
FOR EACH ROW
BEGIN
END//
DELIMITER //
CREATE TRIGGER mytrigger2 AFTER INSERT ON mytable
FOR EACH ROW
BEGIN
END//
If you want to hook more than one action onto the same event/table, you will have to cram all of it into one trigger. You could call many stored procedures like this:
DELIMITER //
CREATE TRIGGER mytrigger1 AFTER INSERT ON mytable
FOR EACH ROW
BEGIN
CALL fromulate_the_moobars(NEW.myid);
CALL its_peanut_butter_jelly_time(NEW.myname);
END//

MySQL trigger to insert records from Table A to Table B

I am unable to get the following code to work, using this page for reference as well as of other posts on this site. I need to create a trigger that will insert a record in Table B whenever Table A is updated. The code below shows what I am attempting; however this produces a syntax error (#1064). I am also unclear on if I need to include the 'DELIMITER $$' syntax or not. I appreciate your help
DELIMITER $$
CREATE TRIGGER MyTrigger
AFTER INSERT
ON TableA
FOR EACH ROW
BEGIN
INSERT INTO TableB SET
TableA_id = NEW.TableB_id,
TableA_date = NEW.TableB_date,
TableA_comment = NEW.TableB_comment;
END;
END $$
DELIMITER ;
EDIT: in the pseudo-code above I am using a $TableName_$FieldName convention to indicate that Column A belongs to Table A, Column B belongs to Table B. I should have made that more clear in my original question. Someone commented below I have the NEW indicator on the wrong side (should be on Table A), but that comment appears to have been removed. Can someone please confirm? Thanks for all your help
Try this:
DELIMITER $$
CREATE TRIGGER MyTrigger
AFTER INSERT
ON TableA
FOR EACH ROW
BEGIN
INSERT INTO TableB SET
TableB_id = NEW.TableA_id,
TableB_date = NEW.TableA_date,
TableB_comment = NEW.TableA_comment;
END $$
DELIMITER ;
The DELIMITER here is used to tell mysql to treat all the following ; as part of the definition instead of actual command terminations.
Notice that I removed an unmatched END; just before the closing END $$
You have an erroneous END; in your trigger (every END should pair with a BEGIN).
For that matter, you don't even need the BEGIN ... END block, since the trigger contains only one statement (and if that block is omitted, you don't even need to change the statement delimiter because no semicolon appears within the CREATE TRIGGER statement):
CREATE TRIGGER MyTrigger AFTER INSERT ON TableA FOR EACH ROW
INSERT INTO TableB SET
TableA_id = NEW.TableB_id,
TableA_date = NEW.TableB_date,
TableA_comment = NEW.TableB_comment

first mysql trigger failing

Ok, I've started writing my first trigger in mysql, it doesn't give an errors, but it doesn't work either...
DELIMITER $$
DROP TRIGGER `cc`.`update_expires_date_trig`$$
CREATE TRIGGER `update_expires_date_trig` BEFORE INSERT ON `credit_test_acc`
FOR EACH ROW BEGIN
UPDATE credit_test_acc SET date_expires_acc = DATE_ADD(CURDATE(), INTERVAL 6 MONTH) WHERE type_acc = 'init'
END;
$$
DELIMITER ;
I have 2 problems:
Can't update table 'credit_test_acc' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Is the trigger as defined going to update JUST the JUST inserted row, or EVERY row in the database?
As far as I know, it must be rewritten like this to work as you expect it to work:
DELIMITER $$
DROP TRIGGER `cc`.`update_expires_date_trig`$$
CREATE TRIGGER `update_expires_date_trig` BEFORE INSERT ON `credit_test_acc`
FOR EACH ROW BEGIN
SET NEW.date_expires_acc = DATE_ADD(CURDATE(), INTERVAL 6 MONTH)
END;
$$
DELIMITER ;
Where NEW refers to the row that is about to be inserted into the table. You didn't give any explanation as to what role 'type_acc' might play here (I can think of more than one way it could be interpreted), so I've left that out. If it is what I think it is, you can apply it like this:
IF NEW.type_acc = 'init' THEN # do whatever you want here
A Trigger cannot change the table that triggered it.
Either directly or indirectly.
You can only change values in a BEFORE trigger by SET new.field = newvalue.
And this can only effect the 'current' row that pulled the trigger (so to speak).