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.
Related
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.
I'm relatively new to MYSQL from an MS SQL background. Can someone please point out why I'm getting syntax errors here please? I'm sure I've done something very obviously wrong but I just can't see it:
I'm getting a missing semicolon syntax error on line 8:
CREATE TRIGGER trg_InsertProductWatchListPriceHistory
AFTER UPDATE
ON ProductWatchlist FOR EACH ROW
BEGIN
INSERT INTO ProductWatchListPriceHistory
(ProductWatchlistID,Price)
VALUES
(ProductWatchlistID,New.ProductPrice);
END;
Any help greatly appreciated.
You need to change your default delimiter to something else than ;. Otherwise your definition ends at the first ; which would make it incomplete.
delimiter |
CREATE TRIGGER trg_InsertProductWatchListPriceHistory
AFTER UPDATE
ON ProductWatchlist FOR EACH ROW
BEGIN
INSERT INTO ProductWatchListPriceHistory (ProductWatchlistID, Price)
VALUES (New.ProductWatchlistID, New.ProductPrice);
END
|
delimiter ;
Please check this bash script for generating the update trigger.
This script will require the database name and table names.
While using this script please update the username and password.
If using MySQL Workbench then Editor's create trigger under table design options should be used to avoid changing delimiters before and after.
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 ;
I have been at this for hours now, and I have tried everything I could find on stackoverflow and the internet. Nothing has worked. I have tried entering the code directly in the SQL prompt on phpmyadmin, as well as tried to create the event in the events tab.
For some odd reason, when giving it a second command, I get a syntax error. Each command on its own is accepted just fine. Together though? Syntax error.
Here's the code.
CREATE EVENT update_stats
ON SCHEDULE EVERY 15 MINUTE
ON COMPLETION PRESERVE ENABLE
DO
BEGIN
UPDATE stats JOIN temp_stats ON stats.unique_key = temp_stats.unique_key
SET stats.clicks = stats.clicks + temp_stats.clicks;
TRUNCATE temp_stats;
END
This yields a syntax error. I found a similar question on stackoverflow (see below) but none of the solutions worked. Yes, I tried setting and using a different delimiter. I even updated phpmyadmin to 4.4.1. Nothing works, just says I have a syntax error. I'm at my wits end here. MySQL 5.6.17.
phpmyadmin|How to create an event do 2 actions
I think this answer is missing an explanation as Jean-Francois was the correct answer but I didn't get it to work until I worked out what the "Delimiter" is.
When sending commands to SQL they are separated with a "Delimiter" the default is ";"
When I was making my event, inside the "CREATE EVENT" statement contains SQL queries inside the "BEGIN" and "END". SO MYSQL, parsing the query, runs into a ';' inside the BEGIN statement and correctly ends the statement there, saying "error in syntax near '' at line X" (the line it found the semi-colon on).
So to get around this you have to change the default delimiter for parsing, then run the query so you can include SQL statements inside your CREATE EVENT query with it ignoring the semi-colon and set the default delimiter back to a semi-colon:
DELIMITER $$
CREATE EVENT `snap_extcrs`
ON SCHEDULE
EVERY 1 DAY STARTS '2020-04-17 23:59:59'
ON COMPLETION PRESERVE
ENABLE
COMMENT ''
DO
BEGIN
INSERT INTO table (field, field2)
SELECT 1, 2 FROM table2
WHERE
date = DATE(NOW());
INSERT INTO table3 (field, field2)
SELECT 1, 2 FROM table2
WHERE
date = DATE(NOW());
END$$
DELIMITER ;
You forgot to put the END delimiter
DELIMITER #
CREATE EVENT update_stats
ON SCHEDULE
EVERY 15 MINUTE
ON COMPLETION PRESERVE ENABLE
DO BEGIN
UPDATE stats JOIN temp_stats ON stats.unique_key = temp_stats.unique_key
SET stats.clicks = stats.clicks + temp_stats.clicks;
TRUNCATE temp_stats;
END#
DELIMITER ;
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...