Error MySQL Creating Trigger - mysql

I'm getting an odd error every time I try to create one UPDATE trigger into one of my tables...
The Trigger:
CREATE TRIGGER upd_data_nascimento BEFORE UPDATE ON pfi_pessoa_fisica
FOR EACH ROW
BEGIN
IF OLD.date_of_birth = '0000-00-00' THEN
SET NEW.date_of_birth = NULL;
END IF;
END;
And the error:
Error SQL (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
Any ideas?

Make sure you are using delimiter
CREATE TRIGGER upd_data_nascimento BEFORE UPDATE ON abc_table
FOR EACH ROW
BEGIN
IF OLD.date_of_birth = '0000-00-00' THEN
SET NEW.date_of_birth = NULL;
END IF;
END
If you are executing it in PHPMyAdmin then set the delimiter to # or something else. And then execute it will work fine I have tested it with above code.

Related

You have an error in your SQL syntax... getting error when creating trigger

I have wrote trigger for my DB.
CREATE TRIGGER executor_type_check BEFORE INSERT ON executors
FOR EACH ROW
BEGIN
IF NEW.points <> 100
SET NEW.points = 0;
END IF;
END
I get the following error upon importing sql file
You have an error in your SQL syntax;
check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SET NEW.points = 0' at line 5
My .sql file has the following structure:
DataBase creation
start transaction
creation of tables
creation of trigger
commit
Without trigger no error is shown.
As per the Comments below your Question.
You are missing THEN after the IF statement. And also need to Add DELIMITER.
Try this:
DELIMITER $$
CREATE TRIGGER executor_type_check BEFORE INSERT ON executors
FOR EACH ROW
BEGIN
IF NEW.points <> 100
THEN
SET NEW.points = 0;
END IF;
END;$$
DELIMITER ;

MySQL syntax Error on Create Trigger

Here is my trigger, I am getting the MySQL syntax error. I wanted to reduce the balance from sms_index table sms_count column value.
CREATE TRIGGER sms_log_update AFTER UPDATE ON sms_index
FOR EACH ROW
BEGIN
IF NEW.approved_status = '1' THEN
UPDATE sms_package SET balance = (balance - OLD.sms_count) WHERE group_id = OLD.ins_group_id;
END IF;
END;
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 '' at line 5
Your code looks correct, except for the possible problem of the delimiter. Try the following:
DELIMITER //
CREATE TRIGGER sms_log_update AFTER UPDATE ON sms_index
FOR EACH ROW
BEGIN
IF NEW.approved_status = '1' THEN
UPDATE sms_package SET balance = (balance - OLD.sms_count)
WHERE group_id = OLD.ins_group_id;
END IF;
END;//
DELIMITER ;
From the documentation:
However, just as for stored routines, if you use the mysql program to define a trigger that executes multiple statements, it is necessary to redefine the mysql statement delimiter so that you can use the ; statement delimiter within the trigger definition.

Error when creating mysql trigger

I want to create a trigger to set the value of f_IrrMinCurr based on the IrrMinCurr. This is what I wrote.
CREATE TRIGGER Fuzzification BEFORE INSERT ON fuzzyanalysis
FOR EACH ROW
BEGIN
IF NEW.IrrMinCurr < 200.0 THEN
SET NEW.f_IrrMinCurr = 'H';
ELSE
SET NEW.f_IrrMinCurr = null;
END IF;
END;
I'm getting the error,
Error
SQL query:
CREATE TRIGGER Fuzzification BEFORE INSERT ON fuzzyanalysis
FOR EACH ROW
BEGIN
IF NEW.IrrMinCurr < 200.0 THEN
SET NEW.f_IrrMinCurr = null;
MySQL said: Documentation
#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
Can anyone please help me to solve this.

Error 1064 during trigger creation

I want to create a trigger on one of the tables, it's very simple, just to set one value - but I always get error 1064
CREATE TRIGGER `trigger_TEST` BEFORE INSERT ON `TEST_table`
FOR EACH ROW
BEGIN
IF NEW.as_src = 13335 THEN
SET NEW.peer_as_src = 13335;
END IF
END
And get this error:
Error SQL (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
Any idea why ?
You need to change the default delimiter
delimiter |
CREATE TRIGGER `trigger_TEST` BEFORE INSERT ON `TEST_table`
FOR EACH ROW
BEGIN
IF NEW.as_src = 13335 THEN
SET NEW.peer_as_src = 13335;
END IF;
END
|
delimiter ;
Otherwise the statement will end at the first ; which would make the trigger definition incomplete.

Adding MySQL Trigger: invisible (to me) Syntax Error

NOTE: I initially thought my problem had to do with CONCAT, but since the exact error persisted even when I completely eliminated CONCAT from my query, I figured I should completely over-write of my original post.
I'm trying to set a trigger, and keep getting a Syntax Error.
When I tried this
CREATE TRIGGER set_aka_name
BEFORE INSERT ON sandbox_person
FOR EACH ROW
BEGIN
IF (NEW.aka IS NULL) THEN
SET NEW.aka = 'test value';
END IF;
END
...I got this 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 '' at line 6
And when trying this (without the IF peren's)
CREATE TRIGGER set_aka_name
BEFORE INSERT ON sandbox_person
FOR EACH ROW
BEGIN
IF NEW.aka IS NULL THEN
SET NEW.aka = 'test value';
END IF;
END
...I get the same exact 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 '' at line 6
It's got to be something simple ... but I'm just not seeing it.
I'm on MySQL 5.1.36.
What am I missing?
Any pointers?
try to change delimiter first:
DELIMITER |
CREATE TRIGGER set_aka_name
BEFORE INSERT ON sandbox_person
FOR EACH ROW
BEGIN
IF NEW.aka IS NULL THEN
SET NEW.aka = 'test value';
END IF;
END|
DELIMITER ;
Maybe MySQL sees the first ";" as the end of CREATE TRIGGER
try this:
CREATE TRIGGER set_aka_name
BEFORE INSERT ON sandbox_person
FOR EACH ROW
BEGIN
IF NEW.aka IS NULL THEN
NEW.aka = CONCAT(NEW.fname, ' ', NEW.lname);
END IF;
END
there is a difference between IF () function and IF statement (see this)