Proper use of BEGIN and END in MySQL - mysql

I just switched from using Apache's Derby Database to MySQL and still getting familiar with the syntax. I read the documentation about triggers and I think I followed the syntax correctly. However, I'm having problems with BEGIN and END My insert trigger below doesn't work if I put BEGIN and ENDI even tried putting DELIMITER but it doesn't fix it.
CREATE TRIGGER COPY_INSERTED_USERID_TO_ALLUSERS_PERMISSIONS
AFTER INSERT ON ALLUSERS
FOR EACH ROW
BEGIN
INSERT INTO ALLUSERS_ADMIN_PERMISSIONS(USERID)
VALUES(NEW.USERID);
END;
Removing the BEGIN and END makes it work but I'm not able to take full advantage of the compound statements.
CREATE TRIGGER COPY_INSERTED_USERID_TO_ALLUSERS_PERMISSIONS
AFTER INSERT ON ALLUSERS
FOR EACH ROW
INSERT INTO ALLUSERS_ADMIN_PERMISSIONS(USERID)
VALUES(NEW.USERID);
I'd appreciate any help.
Thanks.
Edited:
I tried to follow #Ilanatos advice which works but returns an error on first attempt. I had to refresh the phpmyadmin page to get rid of the error.
Below are the screenshots.
then if I refresh the page(both Firefox and Chrome), I see the trigger.
I don't think it should return an error message during execution of create trigger definition.

Try using the delimiter function when creating your trigger.
DELIMITER $$
CREATE TRIGGER COPY_INSERTED_USERID_TO_ALLUSERS_PERMISSIONS
AFTER INSERT ON ALLUSERS
FOR EACH ROW BEGIN
INSERT INTO ALLUSERS_ADMIN_PERMISSIONS(USERID)
VALUES(NEW.USERID);
END$$
DELIMITER ;

Related

Delphi Unidac MySQL Create Trigger From Memo Code Syntax Error

Here is my MySQL Trigger code;
DELIMITER //
CREATE TRIGGER pdfdenemeu BEFORE INSERT
ON denemetbl
FOR EACH ROW
BEGIN
SET NEW.iki = CONCAT(NEW.bir,'.pdf');
END//
DELIMITER ;
When i run this code on HeidiSQL it creates trigger and works perfectly.
On Delphi XE7 i'm adding a button and a memo to form, putting this code inside of the memo, and button's onclick event is;
sorgu.Close;
sorgu.SQL.Clear;
sorgu.SQL.Add(trim(memo1.text));
sorgu.ExecSQL;
When i click to button, it returns syntax error ;
Also tried as below;
sorgu.Close;
sorgu.SQL.Clear;
sorgu.SQL.Add('DELIMITER //');
sorgu.SQL.Add('CREATE TRIGGER pdfdenemeu BEFORE INSERT');
sorgu.SQL.Add('ON denemetbl');
sorgu.SQL.Add('FOR EACH ROW');
sorgu.SQL.Add('BEGIN');
sorgu.SQL.Add('SET NEW.iki = CONCAT(NEW.bir,''.pdf'');');
sorgu.SQL.Add('END//');
sorgu.SQL.Add('DELIMITER ;');
sorgu.Execute;
As i mentioned trigger code works without any error on HeidiSQL and MySQL command line, why i'm getting this error message, what am i doing wrong?
As #olivier mentioned DELIMETER is specific to HeidiSQL, i tried to remove delimeter and tried again but didn't solved the problem, but what i did was call the old code with delimeter from my new code, when i thought i removed the delimeter but i wasn't.
So as #olivier mentioned in first post's comment i changed query to this;
CREATE TRIGGER pdfdenemeu BEFORE INSERT ON denemetbl FOR EACH ROW BEGIN SET NEW.iki= CONCAT(NEW.bir,'.pdf'); END;
Worked perfectly.
#olivier Thank you, i owe you one.

MySQL 5.7 Create Trigger Syntax Error?

I've been trying to create a simple BEFORE INSERT trigger on a database table (MySQL v 5.7 ) but I keep receiving a vague "#1064 ... syntax error" message which doesn't help resolve the issue.
Here's the SQL:
CREATE OR REPLACE TRIGGER `CREATE_QUIZ_TRIG` BEFORE INSERT ON `quiz`
FOR EACH ROW BEGIN
SET NEW.ACTIVE = UPPER(NEW.ACTIVE);
SET NEW.CREATED = NOW();
END
/
All I'm trying to do is enforce a column to uppercase and then insert the current date & time into a timestamp column. I've been following the documentation from:
https://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html
and realise that for multi-statement expression I have to redefine the delimiter at the beginning of the trigger's creation but the same '#1064' error occurs.
This is made even more confusing because when I use phpmyadmin's interface for creating the same trigger it works fine - but won't when I export the generated SQL and try to create the trigger using that!?
Thanks for any help
I didn't realise that, by default, phpmyadmin adds a ; delimiter which was breaking the ; used to end a statement within the BEGIN END block.

Error to create a a trigger

I'm trying to create a trigger that everytime that insert a new data in my table tb_produto_parent, I need to update the columm cod_prod add one more.
Follow the trigger :
DELIMITER $$
create trigger trgAdicionaUm after insert
on tb_produto_parent
for each row
BEGIN
select cod_prod from tb_produto_parent;
update
tb_produto_parent set cod_prod = cod_prod +1;
END;
When I try to execute the code, MySQL show me a error :
Error Code: 1415 Not allowed to return a result set from a trigger.
Thanks !
There are two major problems with your code
you can't use SELECT on it own in a trigger because a trigger doesn't return a resultset to the client
you can't use DML statements (UPDATE in your case) on the same table (tb_produto_parent) on which you have your trigger in MySQL. Therefore even if you fix the first problem you still won't be able to update any row in tb_produto_parent within the trigger.
The only thing you can do in MySQL trigger is to alter values of columns of a row being inserted by using a BEFORE event for a trigger.
A possible solution is to use a stored procedure instead.
Error Code: 1415 Not allowed to return a result set from a trigger.
Looking at your trigger:
BEGIN
select cod_prod from tb_produto_parent;
update
tb_produto_parent set cod_prod = cod_prod +1;
END;
It would seem the select statement is the cause of this error. Remove it.

Unexpected END_OF_INPUT in MySQL trigger

I have searched for all the possible online solutions but I can't figure out the error in this trigger.
CREATE TRIGGER `delete_neat_link`
AFTER DELETE ON `neat_urls`
FOR EACH ROW
BEGIN
DELETE FROM `css_paths`
WHERE `css_paths`.`path_id` = OLD.`neat_link`;
END;
the first error appears at OLD.neat_link
syntax error, unexpected END_OF_INPUT, expecting ';'
and the second one at END;
syntax error, unexpected END
Any help would be appreciable, thanks.
That problem is due to interpreting individual statements. The CREATE TRIGGER statement is as such a single complete statement that must be sent as is to the server. Usually statement borders are recognized by the default delimiter (the semicolon). In case of stored programs however the semicolon is needed to separate inner statements. This would confuse the client as it cannot tell apart what is an inner statement of the stored program or a full statement as it must be sent as a whole to the server.
Hence the DELIMITER statement was introduced which only applies to clients (not the server, the server itself cannot parse this statement). It changes the default delimiter to one of your choice, leading so the client where to look for the statement's end. A typical case hence looks like this:
DELIMITER ;;
CREATE TRIGGER `ins_film` AFTER INSERT ON `film` FOR EACH ROW BEGIN
INSERT INTO film_text (film_id, title, description)
VALUES (new.film_id, new.title, new.description);
END;;
Their is only one statement in the body of the Trigger, so there is no need to use the BEGIN-END compound statement construct. Try this:
CREATE TRIGGER `delete_neat_link`
AFTER DELETE ON `neat_urls`
FOR EACH ROW
DELETE FROM `css_paths`
WHERE `css_paths`.`path_id` = OLD.`neat_link`
another possible solution
DELIMITER $$
CREATE TRIGGER `delete_neat_link`
AFTER DELETE ON `neat_urls`
FOR EACH ROW
BEGIN
DELETE FROM `css_paths`
WHERE `css_paths`.`path_id` = OLD.`neat_link`;
END$$
DELIMITER ;

how to use a block of commands in triggers

The following code works
CREATE
TRIGGER rebuild_course_auto_enrollment_tree_mv AFTER INSERT
ON course_auto_enrollment FOR EACH ROW
DELETE FROM
cron_event_tasks;
If I add the BEGIN ... END as written in the documentation
CREATE
TRIGGER rebuild_course_auto_enrollment_tree_mv AFTER INSERT
ON course_auto_enrollment FOR EACH ROW
BEGIN
DELETE FROM
cron_event_tasks;
END;
This is not working, hmmmm...What am I missing?
Stupid me.
I am using phpmyadmin, and it uses the ; as the delimiter to separate queries.
Solution: change in the query window of phpmyadmin the delimiter to something else and wallah...