Adding MySQL Trigger: invisible (to me) Syntax Error - mysql

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)

Related

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 MySQL Creating Trigger

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.

Error 1064 on Mysql trigger

I'm writing a trigger in MySQL to take log of table updates. The log table is called individuo_storico and the target table is called individuo. When individuo is updated, I want to check if IDQualifica and IDLivello are changed, if yes a record in individuo_storico is inserted.
I write down this code but I get a #1064 error, where's the syntax error?
use ore;
create trigger individuo_update after update on individuo
for each row
begin
if ( NEW.IDLivello <> OLD.IDLivello or NEW.IDQualifica <> OLD.IDQualifica) then
insert into individuo_storico(IDIndividuo, IDQualifica, IDLivello)
values (NEW.IDIndividuo, NEW.IDQualifica, NEW.IDLivello);
end if;
end;
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
Please wrap your trigger creation with the monikers necessary so the db engine does not choke on it. So three areas:
1) line 1
2) right after the end;
3) and then a reset to a default delimiter.
Same concept is typical with stored proc creations.
DELIMITER $$
create trigger individuo_update after update on individuo
for each row
begin
if ( NEW.IDLivello <> OLD.IDLivello or NEW.IDQualifica <> OLD.IDQualifica) then
insert into individuo_storico(IDIndividuo, IDQualifica, IDLivello)
values (NEW.IDIndividuo, NEW.IDQualifica, NEW.IDLivello);
end if;
end;$$
DELIMITER ;

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.

How to create a trigger to set empty stings to null before insert with SQL?

I'm using phpmyadmin and I can't figure to how to get it to automatically set empty strings to null while importing data, so I'm trying to write a trigger to do it.
My trigger will eventually have to include many more fields than this, but this is my test run trigger, which is not working:
create trigger test1
before insert
on hvi
for each row
begin
if new.`Amt` = ' ' then
set new.`Amt` = null
end if;
end;
I get this error:
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 'end if' at
line 8
What am I doing wrong?
if new.`Amt` = ' ' then
set new.`Amt` = null ;
end if;