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.
Related
I have this script for update table monthly:
DELIMITER |
CREATE
EVENT `kpiparams_scheduled_update`
ON SCHEDULE
EVERY 1 MONTH
STARTS '2020-02-01 02:59:59'
ON COMPLETION PRESERVE
COMMENT 'KPIParams was updated by event_kpiparams_scheduled_update'
DO
BEGIN
UPDATE kpiparams INNER JOIN kpiparams_update
ON kpiparams.param_name = kpiparams_update.param_name
SET kpiparams.good = kpiparams_update.good,
kpiparams.bad = kpiparams_update.bad,
kpiparams.weight_gold = kpiparams_update.weight_gold,
kpiparams.weight_tech = kpiparams_update.weight_tech,
kpiparams.is_for_calc = kpiparams_update.is_for_calc
WHERE kpiparams.param_name = kpiparams_update.param_name;
END |
DELIMITER ;
This code drop exception:
Caused by: java.sql.SQLSyntaxErrorException: 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 | CREATE EVENT kpiparams_scheduled_update ON SCHEDULE EVERY 1 MONTH ST' at line 1
And this:
nested exception is java.sql.SQLSyntaxErrorException: 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 | CREATE EVENT kpiparams_scheduled_update ON SCHEDULE EVERY 1 MONTH ' at line 1
I serched some resources that said, I need use Delimeter, don't write amount of months in '' and use BEGIN-END. By the way, it didn't help me.
spring.datasource.schema=classpath*:mysql-scripts/event_update_kpiparams.sql
EDITED SCRIPT:
CREATE
EVENT kpiparams_scheduled_update
ON SCHEDULE EVERY 1 MONTH STARTS '2020-02-01 02:59:59'
ON COMPLETION PRESERVE
COMMENT 'KPIParams was updated by event_kpiparams_scheduled_update'
DO
BEGIN
UPDATE kpiparams INNER JOIN kpiparams_update
ON kpiparams.param_name = kpiparams_update.param_name
SET kpiparams.good = kpiparams_update.good,
kpiparams.bad = kpiparams_update.bad,
kpiparams.weight_gold = kpiparams_update.weight_gold,
kpiparams.weight_tech = kpiparams_update.weight_tech,
kpiparams.is_for_calc = kpiparams_update.is_for_calc
WHERE kpiparams.param_name = kpiparams_update.param_name;
END
I guess you tried to run this using JDBC?
You don't need DELIMITER | at all. That's a mysql client builtin command. Client builtins are not recognized by the SQL parser.
You can just execute the CREATE EVENT statement as a single statement and then you don't need to have a delimiter at the end of the statement. Delimiters are only important in interfaces that support multiple statements (e.g. the mysql client).
Okay it seems you are using multiple statements in an .sql file and you need some way of separating the statements. Normally this is ; but you have some statements that contain ; as part of the statement, not as the separator.
I'm not a Spring developer, but I found Spring Boot Database initialization MySQLException for Trigger which describes the use of:
spring.datasource.separator
This is also documented: https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html
spring.datasource.separator
(default) ;
Statement separator in SQL initialization scripts.
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 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
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 ;
I am trying to make a database that does some inventory checking, and I am working on some triggers to update everything as required. In this case, that I want to do is to take the current availability (disponibilidad) of an ingredient from the ingredients table, add to it how much is taken or added, and then save that value into the table stock_disponibilidad, as you can see from the trigger code I am trying to use.
DELIMITER $$
CREATE TRIGGER `update_disponibilidad_variacion` BEFORE INSERT ON `stock_disponibilidad`
FOR EACH ROW BEGIN
DECLARE old_disp DOUBLE DEFAULT 0;
SELECT disponibilidad INTO old_disp FROM ingredientes WHERE id = NEW.ingredientes_id;
NEW.disponibilidad = old_disp + NEW.variacion;
END$$
DELIMITER ;
However, whenever I execute the query to create the trigger, I get the following 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 '.disponibilidad = old_disp + NEW.variacion; END' at line 5
I've done everything I read about this issue, still without result. What am I doing wrong?