MySQL before update trigger an insert syntax error using PhpMyAdmin - mysql

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.

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”.

Removing "The " with an IF statement in MYSQL Before Insert Trigger

Hello helpful world of Stack Overflow!
I am brand new to Triggers, but am having fun using them in my new project.
I have already gotten a few done, but am struggling with setting up a sort field by removing "The " from the beginning of a name.
Being new to this, I am using PHP MyAdmin to build my triggers. So I have it set to my 'aartist' table, Before Insert. The 2 fields are artist_name & artist_sort. This is the full code I've tried with no luck.
BEGIN
IF (left(new.artist_name,4) != "The ")
THEN
set new.artist_sort = new.artist_name
ELSE
set new.artist_sort = right(new.artist_name,len(new.artist_name)-4)
END IF;
END
I've also tried stripping it back for something more simple, still without luck. The IF statement seems to not work in the TRIGGER.
BEGIN
IF (new.artist_name != new.artist_sort) THEN
set new.artist_sort = new.artist_name
END IF;
END
The Error PHPMyAdmin gives is:
One or more errors have occurred while processing your request:
The following query has failed: "CREATE DEFINER=`####`#`localhost` TRIGGER `AddArtist` BEFORE INSERT ON `aartist` FOR EACH ROW BEGIN IF (new.artist_name != new.artist_sort) THEN set new.artist_sort = new.artist_name END IF; END"
MySQL said: #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 IF; END' at line 4
Thank you, as always, for any help in figure out what I might be doing wrong with this.
Did some more digging around and found some SQL code that I modified, so instead of using the PHPMyAdmin Trigger Form, I just adjusted the SQL code and it worked! Not 100% sure why it worked with this, but the main change was the Delimiter piece in the code and additional ;s, so my guess is that had something to do with it.
MySQL trigger if condition exists
DROP TRIGGER IF EXISTS AddArtist;
DELIMITER $$
CREATE TRIGGER AddArtist BEFORE UPDATE ON `aartist`
FOR EACH ROW BEGIN
IF (left(NEW.artist_name,4) = 'The ') THEN
SET NEW.artist_sort = right(NEW.artist_name, CHAR_LENGTH(NEW.artist_name) - 4);
ELSE
SET NEW.artist_sort = NEW.artist_name;
END IF;
END$$
DELIMITER ;

the error with trigger in MySQL 5.1.63-community

I write a trigger in MySQL 5.1.63-community , it's always note:#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 6 ,but I run the code at navicat for mysql ,it's ok.
code here:
CREATE TRIGGER trig_sign
AFTER insert on students
for each row
Begin
INSERT INTO sign_in(num,uptime,flag,sid)
values(null,now(),false,NEW.sid);
end;
If the trigger body contains only one statement you don't need to use BEGIN and END:
CREATE TRIGGER trig_sign AFTER INSERT on students FOR EACH ROW
INSERT INTO sign_in(num,uptime,flag,sid) VALUES(null,now(),false,NEW.sid);
For multiple statement triggers you need to switch the delimiter:
DELIMITER //
CREATE TRIGGER trig_sign
AFTER INSERT on students
FOR EACH ROW
BEGIN
INSERT INTO sign_in(num,uptime,flag,sid)
VALUES(null,now(),false,NEW.sid);
-- more statements
END //
DELIMITER ;
You have to re-define delimiter like this:
delimiter ||
CREATE TRIGGER trig_sign
AFTER insert on students
for each row
Begin
INSERT INTO sign_in(num,uptime,flag,sid)
values(null,now(),false,NEW.sid);
end ||
delimiter;
Also you can see official doc about delimiter here.
everyone who have the some problem with me ,you can follow the code by Paul Spiegel .If it don't work ,I think your mysql version also don't support "delimiter ",I advise you change version.

simple mysql trigger, cannot find the syntax error. appreciate for it

I am using phpmyadmin and got a syntax error from my trigger part. I followed examples online but didn't see anything wrong. Does anyone see the syntax error? Thank you in advance.
Here is my code:
CREATE TRIGGER INSERT_T BEFORE INSERT ON dataT
for each row begin
IF(NEW.id IS NOT NULL)
SET NEW.id = 9999;
END IF
end;
mysql reports that:
#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 'SET NEW.id = 9999' at line 4
.
Missing THEN keyword.
MySQL Reference Manual:
IF Syntax https://dev.mysql.com/doc/refman/5.7/en/if.html
And for your next questions. Yes, you need a semicolon following the END IF
And you need to specify a delimiter other than the default semicolon. e.g.
DELIMITER $$
CREATE TRIGGER ...
END$$
DELIMITER ;

MySQL CREATE TRIGGER and DELIMITER ISSUES

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?