MySQL syntax error on SET when defining a trigger - mysql

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!

Related

MySql Syntax error when creating trigger to insert calculated value

This is my first time trying to create a MySql trigger but I'm running into syntax errors that I can't identify. I am trying to have the trigger insert a calculated value when a row is updated. Below is my code, but I keep getting syntax errors when I try to execute it, and I cannot see where the error is. Can someone please look, what am I doing wrong?
DROP TRIGGER ins_cop_dhw;
CREATE TRIGGER ins_cop_dhw BEFORE INSERT ON `2017010001_data`
FOR EACH ROW
BEGIN
IF (NEW.`_007E` = 1 AND `_00A2` > 0) THEN
SET NEW.`_00B7` = NEW.`_0096` / NEW.`_00A2`;
END IF;
IF (NEW.`_007D` = 1 AND `_00A2` > 0) THEN
SET NEW.`_00B8` = (NEW.`_0096` / NEW.`_00A2`);
END IF;
END;
Here is the error I get in MySQL Workbench, but it's not much help. :/
Error Code: 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 5
MySQL Workbench highlights the end of line 6 as the start of the syntax error.
As said by #Akina I was missing the delimiter statements. Here is fixed code.
delimiter //
DROP TRIGGER ins_cop_dhw;
CREATE TRIGGER ins_cop_dhw BEFORE INSERT ON `2017010001_data`
FOR EACH ROW
BEGIN
IF (NEW.`_007E` = 1 AND NEW.`_00A2` > 0) THEN
SET NEW.`_00B7` = NEW.`_0096` / NEW.`_00A2`;
END IF;
IF (NEW.`_007D` = 1 AND NEW.`_00A2` > 0) THEN
SET NEW.`_00B8` = (NEW.`_0096` / NEW.`_00A2`);
END IF;
END//
delimiter ;
As per the advice of #Akina I read the Create Procedure documentation from the dev.mysql.com site and the below quote highlighted the importance of using delimiter
The example uses the mysql client delimiter command to change the statement delimiter from ; to // while the procedure is being defined. This enables the ; delimiter used in the procedure body to be passed through to the server rather than being interpreted by mysql itself. See Section 25.1, “Defining Stored Programs”.

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.

MySQL before update trigger an insert syntax error using PhpMyAdmin

CREATE TRIGGER question_preserver BEFORE UPDATE ON bank
FOR EACH ROW
BEGIN
IF TRIM(NEW.question) != TRIM(OLD.question) THEN
INSERT INTO bank_question_history (id,old_question) VALUES (OLD.id,OLD.question)$$
END IF$$
END$$
I am inserting that query into Mysql using PHPMyAdmin's SQL window, using a delim of $$. I get an error 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 5
I'm sure it's something obvious and I'm just missing it, but no matter what I try I can't get it to work. The error is not helpful at all, and from what I have researched I am doing this exactly like 4-5 examples I found.
Any help would be greatly appreciated, thank you!
Go figure I figured it out right after asking.
CREATE TRIGGER question_preserver BEFORE UPDATE ON bank
FOR EACH ROW
BEGIN
IF TRIM(NEW.question) != TRIM(OLD.question) THEN
INSERT INTO bank_question_history (id,old_question) VALUES (OLD.`id`,OLD.`question`);
END IF;
END$$
You have to use ; to break each statement/command and your delim $$ to end the entire trigger.

What is wrong with my syntax when creating an event? phpmyadmin

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 ;

Trigger only if Table if updated

I want to trigger the table after update only if any change is made in the table.
I tried this
but it is giving error.
My code is
CREATE TRIGGER Trans_SubCategory_update AFTER UPDATE ON Trans_SubCategory
FOR EACH ROW
BEGIN
IF NEW.ts <> OLD.ts THEN
INSERT INTO Sync_activities (table_name,table_id,admin_id,action)
VALUES('Trans_SubCategory',New.id,New.admin_id,'update');
END IF;
END;
It is giving error
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 .
But if I use
CREATE TRIGGER Trans_SubCategory_update AFTER UPDATE ON Trans_SubCategory
FOR EACH ROW
INSERT INTO Sync_activities (table_name,table_id,admin_id,action)
VALUES('Trans_SubCategory',New.id,New.admin_id,'update');
I add the trigger but triggers even if no change is made in table.
The thing is - you're forgetting to set proper delimiter. Your first syntax contains multiple operators inside trigger - and they need to be delimited by ; - but you're not changing global delimiter - thus, MySQL treats that as syntax end - and, therefore, that's an error (because trigger isn't completed, obviously).
Your second syntax is executed completely since it has only one operator (and that stands for trigger end as well) - you've not enclosed it by BEGIN..END
To fix the issue, just do:
DELIMITER //
CREATE TRIGGER Trans_SubCategory_update AFTER UPDATE ON Trans_SubCategory
FOR EACH ROW
BEGIN
IF NEW.ts != OLD.ts THEN
INSERT INTO Sync_activities (table_name,table_id,admin_id,action) VALUES ('Trans_SubCategory', NEW.id, NEW.admin_id,'update');
END IF;
END;//
DELIMITER ;