The second Insert does not work in my trigger - mysql
I am trying to learn more about triggers and I wrote a simple SQL code in MySQL.
This code will work with one insert but it will cause an error when I add the second insert.
I think the code that I wrote does not compile the second insert.
I would be appreciate for Any Help
Code:
create trigger MasterInputTRG
after insert on MasterInput
for each row
insert into NewEMCtestPLAN(ProjectNumber,Manufacture,ModelName,`PO Number`,AccountExecutive,AENotes) values(NEW.ProjectNumber,NEW.Applicant,NEW.ModelName,NEW.PONumber,NEW.AccountExecutive,NEW.AENotes);
insert into NewRF(ProjectNumber,Applicant,ModelName,PONumber,AccountExecutive,AENotes) values(NEW.ProjectNumber,NEW.Applicant,NEW.ModelName,NEW.PONumber,NEW.AccountExecutive,NEW.AENotes);
Error:
Syntax error near 'insert into
NewEMCtestPLAN(ProjectNumber,Applicant,ModelName,PONumber,AccountExe'
at line 5
Update:
I got an answer and I add the other code with errors here.:
Second Code that does not work:
create trigger MasterInputTRG
after insert on MasterInput
for each row
BEGIN
insert into NewEMCtestPLAN(ProjectNumber,Manufacture,ModelName,`PO Number`,AccountExecutive,AENotes) values(NEW.ProjectNumber,NEW.Applicant,NEW.ModelName,NEW.PONumber,NEW.AccountExecutive,NEW.AENotes);
insert into NewRF(ProjectNumber,Applicant,ModelName,PONumber,AccountExecutive,AENotes) values(NEW.ProjectNumber,NEW.Applicant,NEW.ModelName,NEW.PONumber,NEW.AccountExecutive,NEW.AENotes);
END
Errors:
Error in query (1054): Unknown column 'NEW.ProjectNumber' in 'field
list'
But this error did not appear when i used the new.projectnumber in first insert.
second error:
Error in query (1064): Syntax error near 'END' at line 1
A trigger body with multiple statements need to be surrounded bei BEGIN and END. But then (depending on your client) you might need to change the delimiter:
delimiter //
create trigger MasterInputTRG
after insert on MasterInput
for each row
begin
insert into NewEMCtestPLAN (...) values (...);
insert into NewRF (...) values (...);
end //
delimiter ;
The same applies to stored procedures and functions.
Note that the CREATE TRIGGER statement is a single.. well.. statement. Statements are delimited by.. well.. a delimiter. So in your original query the trigger definition ends with the first delimiter (;):
create trigger MasterInputTRG
after insert on MasterInput
for each row
insert into NewEMCtestPLAN (...) values (...); -- trigger ends here
-- anything else is outside the trigger
insert into NewRF (...) values (...);
The second insert could be a valid statement but outside the trigger definition. However I would expect a different error message like: unknown table (alias) 'NEW', because NEW is not known outside the trigger.
Now if we change the delimiter to //, the create statement ends after the first occurance of //, so we can use ; in the trigger body without terminating the CREATE statement.
Usually // or $$ are used for a delimiter. You can try something else, but it should not have any meaning in (My)SQL. For example | is a bit operator.
You should add Begin End in the trigger.
create trigger MasterInputTRG
after insert on MasterInput
for each row
BEGIN
insert into NewEMCtestPLAN(ProjectNumber,Manufacture,ModelName,`PO Number`,AccountExecutive,AENotes) values(NEW.ProjectNumber,NEW.Applicant,NEW.ModelName,NEW.PONumber,NEW.AccountExecutive,NEW.AENotes);
insert into NewRF(ProjectNumber,Applicant,ModelName,PONumber,AccountExecutive,AENotes) values(NEW.ProjectNumber,NEW.Applicant,NEW.ModelName,NEW.PONumber,NEW.AccountExecutive,NEW.AENotes);
END
Related
MySQL Error 1064 42000 while trying to create a trigger
CREATE TRIGGER moving_average AFTER INSERT ON filtered_data FOR EACH ROW BEGIN INSERT INTO moving_average_table (pollutant_id,pollutant_value,lastUpdated,station_id) VALUES (new.pollutant_id,avg(new.pollutant_value),new.lastUpdated,new.station_id) END; I am trying to create a trigger on the table filtered_data with this piece of code, is it syntactically wrong? I wonder why I keep on getting a syntax error 1064 42000. Any help would be appreciated.
I tried this, it looks work for me, delimiter // CREATE TRIGGER moving_average AFTER INSERT ON filtered_data FOR EACH ROW begin INSERT INTO moving_average_table (pollutant_id,pollutant_value,lastUpdated,station_id) VALUES (new.pollutant_id,avg(new.pollutant_value),new.lastUpdated,new.station_id) ; end// delimiter ; according to this https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html if you use the mysql program to define a trigger that executes multiple statements, it is necessary to redefine the mysql statement delimiter so that you can use the ; statement delimiter within the trigger definition. you should remove the begin...end or redefine the delimiter
Make a trigger that copies new data from table to another table after insert
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.
Using MySQL Database Triggers
I have three tables: users, account and accountinfo and I am trying to make a trigger that will add the id from users to the UserID column in the account table. Here is what I tried: CREATE TRIGGER catchUser BEFORE INSERT ON defaultdatabase.users FOR EACH ROW BEGIN INSERT INTO defaultdatabase.account(UserID) VALUES (new.id); END However, I get an error right after my INSERT statement that says, Syntax Error: insert 'semicolon' Why am I getting this error is I have the semicolon or is my trigger just wrong? I'm using MySQL 5.6 if that makes any difference as well.
You need to specify the delimiter: delimiter // CREATE TRIGGER catchUser BEFORE INSERT ON defaultdatabase.users FOR EACH ROW BEGIN INSERT INTO defaultdatabase.account(UserID) VALUES (new.id); END; // delimiter ;
Try this: CREATE TRIGGER catchUser BEFORE INSERT ON defaultdatabase.users FOR EACH ROW BEGIN INSERT INTO defaultdatabase.account(UserID) VALUES (new.id) END;
Please insert a semicolon after END.
Can't create a trigger to insert concat value
i am trying to create a trigger to concatenate my table columns into a single column but i can't find the error. code: create trigger molecule_trigger After insert on molecule For each row begin Update molecule Set molecule_text= CONCAT(mid,',',ULCHEm_ID,',',IUPAC_name,',',Inchi,',',inchi_key,',',smiles,',',can_smiles,',',Molecular_formula,',',Molecular_weight,',',vendor,',',CAS,',',links,',',image); end; ERROR: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 6
You get this error, because you started a "multiple statements" block with begin, but the ; after your update statement terminates the create trigger statement before the end; statement. You have to either change the delimiter DELIMITER $$ create trigger molecule_trigger After insert on molecule For each row begin Update molecule Set molecule_text= CONCAT(mid,',',ULCHEm_ID,',',IUPAC_name,',',Inchi,',',inchi_key,',',smiles,',',can_smiles,',',Molecular_formula,',',Molecular_weight,',',vendor,',',CAS,',',links,',',image); end $$ DELIMITER ; Or you remove the begin and end. create trigger molecule_trigger After insert on molecule For each row Update molecule Set molecule_text= CONCAT(mid,',',ULCHEm_ID,',',IUPAC_name,',',Inchi,',',inchi_key,',',smiles,',',can_smiles,',',Molecular_formula,',',Molecular_weight,',',vendor,',',CAS,',',links,',',image); Now, you have another problem. You're trying to do an action in the trigger on the same table as your trigger works on. This is not allowed. Change your trigger to this: create trigger molecule_trigger BEFORE insert on molecule For each row SET NEW.molecule_text= CONCAT_WS(',', NEW.mid, NEW.ULCHEm_ID, NEW.IUPAC_name, NEW.Inchi, NEW.inchi_key, NEW.smiles, NEW.can_smiles, NEW.Molecular_formula, NEW.Molecular_weight, NEW.vendor, NEW.CAS, NEW.links, NEW.image); Note though, that this sets the molecule_text only for the columns inserted. Your trigger updated the whole table each time a row is inserted. And if you insert 3 rows in one statement, your table gets updated 3 times. This is not what you want to do anyway :)
Incorrect syntax in trigger
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)