I am a rather simple trigger,
CREATE DEFINER = CURRENT_USER TRIGGER 'nfl`.`games_AFTER_INSERT` AFTER
INSERT ON `games` FOR EACH ROW
BEGIN
UPDATE nfl.teams t
SET t.passingyards = t.passingyards +:new.homepassingyards,
t.rushingyards = t.rushingyards +:new.homerushingyards
WHERE t.teamname =:new.hometeamname;
END
however I keep getting a weird syntax error :
"+" is not valid at this position for this server version, expecting '-', '*', '/', '%', '<<','>>',...
I am running MySQL version 8.0.11 and the default storage engine of InnoDB. I searched the MySQL version and triggers are supported by this version so I'm quite lost here. Is this a problem with how the trigger is declared at the top? I'm not used to the syntax "CREATE DEFINER" and thought this might be the problem however it is what is generated by MySQL Workbench when you try to create a new trigger.
Thanks
A simple syntax error, beside the leading colons, which have to be removed. You used different quotes for your first nfl occurance. Here's the correct form:
CREATE DEFINER = CURRENT_USER TRIGGER `nfl`.`games_AFTER_INSERT` AFTER INSERT ON `games` FOR EACH ROW
BEGIN
UPDATE nfl.teams t
SET t.passingyards = t.passingyards + new.homepassingyards,
t.rushingyards = t.rushingyards + new.homerushingyards
WHERE t.teamname = new.hometeamname;
END
Related
I'm trying to create a trigger to import a field from another table when a new row is inserted but get the error message:
#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 'END' at line 6
I'm using the sql tab in phpmyadmin to enter the code.
I've read all the related questions on here, perused several other sites with articles on mysql triggers but can't find anything that helps.
I've tried with // at the end of the line prior to END//
I've tried with ; at the end of the line prior to END//
Nothing seems to work.
My trigger code:
DELIMITER //
CREATE TRIGGER towns2_before_insert
BEFORE INSERT ON towns2 FOR EACH ROW
BEGIN
SET NEW.regionid = (Select RegionID from world_map
where (world_map.X = new.mapx) AND (world_map.Y = new.mapy))
END//
DELIMITER ;
Both towns2 and world_map tables exist and have the required columns.
As far as I can tell this should fetch the RegionID field from any row in the world_map table that has X & Y fields matching the new mapx and mapy columns but all I can get is this error.
No doubt I've got a really simple error in this somewhere but for the life of me I can't find it and all help is greatfully recieved.
UPDATE
Re-created the code as shown by #Chris J below in the myphpadmin trigger form, ignored the big red X against the END statement and hit the GO button - it worked so I guess the problem was not with my code but with my chosen method of creating the trigger. The code as created by phpmyadmin is shown below:
CREATE TRIGGER `towns2_before_insert` BEFORE INSERT ON `towns2`
FOR EACH ROW BEGIN
SET NEW.regionid =
(Select RegionID from world_map
where (X = new.mapx) AND (Y = new.mapy));
END
Although you've mentioned it already, the following works perfectly for me:
DELIMITER //
CREATE TRIGGER towns2_before_insert
BEFORE INSERT ON towns2 FOR EACH ROW
BEGIN
SET NEW.regionid = (Select RegionID from world_map
where (world_map.X = new.mapx) AND (world_map.Y = new.mapy));
END//
This is based on a guess of your schema from your query. The trigger now exists and is visible in Heidi as follows:
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 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 ;
OK something weird is going on and I can't figure it out. I'm creating some triggers, but I get strange errors when trying to create them on the live DB. A sample query is this:
delimiter |
CREATE TRIGGER debug_upd_before BEFORE UPDATE ON properties_availability
FOR EACH ROW BEGIN
INSERT INTO debug_upd_before SET timestamp = NOW(), avID = OLD.avID, avPropertyID = OLD.avPropertyID,
avAvailableFrom = OLD.avAvailableFrom, avAvailableTo = OLD.avAvailableTo, avPrice = OLD.avPrice,
avIsAvailable = OLD.avIsAvailable;
END|
...
which executes fine localy. I'm using phpmyadmin, cause I don't have any other access to the online DB. I know I don't have full permissions and think that it might have something to do with that, but the error I get is:
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 'delimiter |
not "you don't have permissions to do that" or something like that. The same happens when I try to execute something simpler like:
delimiter |
select 1|
select 2
The error I get is the same and I can't set a delimiter and the default doesn't work for me cause I have to use after the queries inside the triggers. I tried other symbols as delimiters, but the result is the same. Any ideas as to what the problem is and how to avoid it? Some of the triggers execute more than one query, if that makes a difference...
Thanks!
Try this statement (without delimiters) -
CREATE TRIGGER debug_upd_before BEFORE UPDATE ON properties_availability
FOR EACH ROW
INSERT INTO debug_upd_before SET
timestamp = now(),
avID = OLD.avID,
avPropertyID = OLD.avPropertyID,
avAvailableFrom = OLD.avAvailableFrom,
avAvailableTo = OLD.avAvailableTo,
avPrice = OLD.avPrice,
avIsAvailable = OLD.avIsAvailable;
The problem is that | is a bitwise or.
So you're using a reserved symbol as a delimiter.
Use $$ instead.
I can't figure out why this trigger creation syntax fails:
CREATE TRIGGER mytrigger BEFORE UPDATE ON mytable
FOR EACH ROW
BEGIN
IF NEW.col1 = 0 AND NEW.col2 != '' AND NEW.col3 > 0 THEN
SET NEW.col1 = NEW.col3 - (10 * 60);
END IF;
END;
MySQL says there's a syntax error at line 5, just before/on the SET statement. I'm using MySQL 5.0.27. I can't see what's wrong, seeing as it's pretty much identical to the example given in the manual 3/4s down.
PS: I'm entering this in the SQL tab on PhpMyAdmin. Adding "delimiter" statements doesn't help. Any clues? Thanks in advance!
I found it. Apparently PhpMyAdmin ignores MySQL's delimiter command when entered as part of an SQL query. Instead, you need to set the delimiter in a separate form field below the SQL query. I totally missed that!