Error 1064 during trigger creation - mysql

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.

Related

Syntax error during creation of trigger on mySQL 10.3.17-MariaDB-log version

I'm trying to add a trigger that should automatically update a datetime column only if is not present a SQL variable. The trigger code is the following
delimiter |
CREATE TRIGGER update_sync_date
BEFORE UPDATE ON posts
FOR EACH ROW
BEGIN
IF #disable_sync_date_update IS NULL OR #disable_sync_date_update <> 1 THEN
SET new.sync_date = CURRENT_TIMESTAMP
END IF
END|
delimiter ;
The error that I recieve is
#1064 - 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 'END IF
END' at line 7
Thanks for helping
I've found the solution, here is the correct code:
delimiter |
CREATE TRIGGER update_sync_date
BEFORE UPDATE ON posts
FOR EACH ROW
BEGIN
IF #disable_sync_date_update IS NULL OR #disable_sync_date_update <> 1 THEN
SET new.sync_date = CURRENT_TIMESTAMP;
END IF;
END|
delimiter ;

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 ;

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)

Mysql create trigger 1064 error

The sql i wrote
**delimiter |
CREATE
DEFINER=CURRENT_USER
TRIGGER set_profiletype_after_insert BEFORE INSERT ON trl_translator FOR EACH ROW
BEGIN
UPDATE trl_profile SET trl_profile.type = 'translator' WHERE trl_profile.profile_id = NEW.translator_id
END
|
delimiter ;**
The error given
[SQL]
**CREATE
DEFINER=CURRENT_USER
TRIGGER set_profiletype_after_insert BEFORE INSERT ON trl_translator FOR EACH ROW
BEGIN
UPDATE trl_profile SET trl_profile.type = 'translator' WHERE trl_profile.profile_id = NEW.translator_id
END
;
[Err] 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**
How to solve this ? where is the mistake i do ?
It looks like you simply need to terminate your UPDATE statement with a semicolon.
Try this code
delimiter |
CREATE
DEFINER=CURRENT_USER
TRIGGER set_profiletype_after_insert BEFORE INSERT ON trl_translator FOR EACH ROW
BEGIN
UPDATE trl_profile SET trl_profile.type = 'translator' WHERE trl_profile.profile_id = NEW.translator_id;
END;|
delimiter ;