I have created this trigger that inserts a record into another table once a record has been inserted in another table
DELIMITER $$
CREATE TRIGGER logan AFTER INSERT ON messagein
FOR EACH ROW
BEGIN
DECLARE le_number INT;
SET le_number = (select messagefrom from messagein where id=NEW.id);
insert into messageout (MessageTo,MessageText) VALUES(le_number,"Thank you for contacting our company.Our sales representatives shall be in touch with you soon.");
END $$
DELIMITER ;
This trigger is created successfully but no message is inserted into the messageout table.What is the reason for this?.
This trigger is wrong, it contains two errors:
quotes are used in a string literal instead of apostrophes
triggers cannot access a table (for reading or writing) that caused the trigger invovation (see this link: http://dev.mysql.com/doc/refman/5.7/en/faqs-triggers.html#qandaitem-B-5-1-9)
Try this code:
CREATE TRIGGER logan AFTER INSERT ON messagein
FOR EACH ROW
BEGIN
insert into messageout (MessageTo,MessageText)
VALUES(NEW.messagefrom,'Thank you for contacting our company.Our sales representatives shall be in touch with you soon.');
END
A link to working demo: http://sqlfiddle.com/#!2/bdbff/1
Related
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 :)
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.
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//
this is the query that i am using
create trigger trig1 after insert on participant for each row
begin
insert into team(sap) select sap from participant order by ID desc limit 1,1
end;
it is supposed to copy the sap field from the participant table into the sap field of the team table after a new row is inserted into the participant table
the engine shows me an unexpected end of input error at the end of "end"
i've tried numerous methods to rework the query but i keep getting the same error
what am i doing wrong?
thanks
You are using trigger than no need to run a query on same table to get latest sap value, you can directly get that value using new.sap.
Problem in your query, In your query you haven't put semicolon(;) after INSERT..SELECT query and END keyword.
This will work for you:
DELIMITER $$
DROP TRIGGER /*!50032 IF EXISTS */ `trig1`$$
CREATE
TRIGGER `trig1` AFTER INSERT ON `participant`
FOR EACH ROW BEGIN
INSERT INTO team(sap)
VALUES(new.sap);
END;
$$
DELIMITER ;
I have two tables called account and transaction. I need to insert data to transaction automatically when account inserts data. I created this trigger in MySQL. It gives error 1064 (Syntax error). What is the problem?
CREATE TRIGGER upTransaction AFTER INSERT ON account FOR EACH ROW
BEGIN
INSERT INTO transaction VALUES (2,account.openDate,CURTIME(),
account.deposit,account.accNo,
"Teller","Cash","Deposit");
END
MySQL gets confused with semicolons inside the BEGIN END block, so you must use DELIMITER to temporarily force MySQL to use a different delimiter.
Documentation: http://dev.mysql.com/doc/refman/5.5/en/create-trigger.html
DELIMITER //
CREATE TRIGGER upTransaction AFTER INSERT ON account FOR EACH ROW
BEGIN
INSERT INTO transaction VALUES (2,account.openDate,CURTIME(),account.deposit,account.accNo,"Teller","Cash","Deposit");
END//
DELIMITER ;
You probably need to replace:
INSERT INTO transaction VALUES (2,account.openDate,CURTIME(),account.deposit,account.accNo,"Teller","Cash","Deposit");
with:
INSERT INTO transaction VALUES (2,NEW.openDate,CURTIME(),NEW.deposit,NEW.accNo,"Teller","Cash","Deposit");
Here is you original trigger
CREATE TRIGGER upTransaction AFTER INSERT ON account FOR EACH ROW
BEGIN
INSERT INTO transaction VALUES (2,account.openDate,CURTIME(),
account.deposit,account.accNo,
"Teller","Cash","Deposit");
END
Replace account. with NEW. (optional : replace " with ')
DELIMITER $$
CREATE TRIGGER upTransaction AFTER INSERT ON account FOR EACH ROW
BEGIN
INSERT INTO transaction VALUES (2,NEW.openDate,CURTIME(),
NEW.deposit,NEW.accNo,
'Teller','Cash','Deposit');
END $$
DELIMITER ;
CREATE
TRIGGER `event_name` AFTER INSERT
ON `database`.`table`
FOR EACH ROW BEGIN
-- trigger body
-- this code is applied to every
-- inserted/updated/deleted row
END;
Seems like you're missing a table name after 'account'. (edit: or a database name before account, I'm not certain as to what account pertains to)