Create trigger syntax error while inserting for trigger action - mysql

I am trying to create a trigger such that if there is any change in a particular table column then event should be created in another. I am using following sql query for this
CREATE TRIGGER enabled_update BEFORE UPDATE ON useraccount
FOR EACH ROW
BEGIN
IF (NEW.enabledAccount != OLD.enabledAccount) THEN
INSERT INTO logEvents
SET NEW.event = OLD.employeeID;
END IF;
END;
But I am getting following error when I am trying to run this query.
Error in query (1064): Syntax error near '' at line 6 and Error in query (1064): Syntax error near 'END IF' at line 1. Where I am mistake?

Use the DELIMITER command, see 23.1 Defining Stored Programs.
DROP TRIGGER IF EXISTS `enabled_update`;
DELIMITER //
CREATE TRIGGER `enabled_update`
BEFORE UPDATE ON `useraccount`
FOR EACH ROW
BEGIN
IF (NEW.`enabledAccount` != OLD.`enabledAccount`) THEN
INSERT INTO `logEvents`
SET `event` = OLD.`employeeID`;
END IF;
END//
DELIMITER ;

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 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 ;

MySQL before insert trigger filling omitted column

I am trying to make make a trigger, that will fill column B with value from column A if column B was not explicitly set in insert query. (column B is set to allow NULL and to default to NULL value)
DELIMITER $$
CREATE TRIGGER trigger_name BEFORE INSERT ON my_table FOR EACH ROW
BEGIN
IF (NEW.valueB IS NULL) THEN SET NEW.valueB = NEW.valueA ;
END
$$
But I am getting this error (not very helpful).
ERROR 1064 (42000): 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 4
When I tried to locate this problematic empty string '' making the query like one word per line, mysql marked the line with '=' character as problematic.
I double checked the query for any non-ascii characters.
I am using mysql version 5.5.37-0ubuntu0.12.10.1 through commandline (eg not phpmyadmin).
You need to close the IF with END IF
DELIMITER $$
CREATE TRIGGER trigger_name BEFORE INSERT ON my_table
FOR EACH ROW
BEGIN
IF NEW.valueB IS NULL THEN
SET NEW.valueB = NEW.valueA ;
END IF ;
END;$$
delimiter ;
Check the example here http://dev.mysql.com/doc/refman/5.5/en/trigger-syntax.html
You forgot to close the IF statement. Read the syntax here. I think the code below will work for you.
DELIMITER $$
CREATE TRIGGER trigger_name BEFORE INSERT ON my_table
FOR EACH ROW
BEGIN
IF (NEW.valueB IS NULL) THEN
SET NEW.valueB = NEW.valueA ;
END IF
END $$
DELIMITER

SQL trigger giving error

I'm trying to call a stored procedure to create multiple rows using some of the cell values of the new row inserted in the table resource but I'm getting an error. Here is my query:
CREATE TRIGGER `details` AFTER INSERT ON `resource`
FOR EACH ROW BEGIN
CALL addcopies(NEW.copies, NEW.id, NEW.location);
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 3
What am I doing wrong?
delimiter |
CREATE TRIGGER `details` AFTER INSERT ON `resource`
FOR EACH ROW BEGIN
CALL addcopies(NEW.copies, NEW.id, NEW.location);
END
|
delimiter ;
If you don't set another delimiter than ;, then the statement will end at the first ; and your trigger definition will be incomplete. You need to tell MySQL that the statement should end at the delimiter you defined. After that you can set the delimiter back with delimiter ;