I'm very new to SQL and am having issues with syntax in my triggers. I am trying to set a minimum rating of 1 and a maximum rating of 5 for horses after a race. The problem seems to be with the IF statement.
Any help would be greatly appreciated.
CREATE TRIGGER new_rating
AFTER insert on stats
FOR EACH ROW
BEGIN
IF (new.rating > 5) THEN
SET new.rating = 5
ELSEIF (new.rating < 1) THEN
SET new.rating = 1
END IF;
END; //
ERROR 1362 (HY000): Updating of NEW row is not allowed in after trigger
This is the error. It means you can't use SET to change any of the values of the row you are inserting, because in an AFTER INSERT trigger, the row has already been inserted. You would have to do this in a BEFORE INSERT trigger for this to work.
You should read https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html which says:
Such a SET statement has no effect in an AFTER trigger because the row change will have already occurred.
If I correct this and change it to a BEFORE INSERT trigger, I get this error:
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 'ELSEIF (new.rating < 1) THEN ...
I notice you did not end the SET statement with a semicolon. This is necessary to terminate every statement in the trigger, including SET statements.
Here's the trigger that works:
CREATE TRIGGER new_rating BEFORE insert on stats
FOR EACH ROW BEGIN
IF (new.rating > 5) THEN
SET new.rating = 5;
ELSEIF (new.rating < 1) THEN
SET new.rating = 1;
END IF;
END
Related
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 ;
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.
Any ideas what's wrong with this query? I searched around stackoverflow and it seems everyone is using this and it works.
mysql> CREATE TRIGGER upd_entry
-> AFTER UPDATE ON entry FOR EACH ROW
-> BEGIN
-> UPDATE entry
-> SET count = count +1
-> WHERE id = NEW.id
-> END;
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 'END' at line 7
Run this first to change the delimiter
DELIMITER //
create the trigger
CREATE TRIGGER upd_entry
AFTER UPDATE ON entry FOR EACH ROW
BEGIN
UPDATE entry
SET count = count +1
WHERE id = NEW.id;
END//
After change it back
DELIMITER ;
forget ; at end of line
CREATE TRIGGER upd_entry
AFTER UPDATE ON entry FOR EACH ROW
BEGIN
UPDATE entry SET count = count +1 WHERE id = NEW.id;//forget to close line ;
END;//
delimiter;
Mysql Trigger
I receive this 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 6" but can not figure out what is wrong.
(position and points are MEDIUMINT, they are not primary key neither unique)
Anyone?
CREATE TRIGGER pointsAssigns
before INSERT ON MyTable
FOR EACH ROW
BEGIN
IF NEW.position>6 THEN
set NEW.points=5;
END IF;
END;
As #Mihai mentioned either add closing END and change the DELIMITER
DELIMITER //
CREATE TRIGGER pointsAssigns
BEFORE INSERT ON MyTable
FOR EACH ROW
BEGIN
IF NEW.position > 6 THEN
SET NEW.points = 5;
END IF;
END //
DELIMITER ;
Here is a SQLFiddle demo
or make it one-line trigger and then you don't need neither BEGIN...END block nor changing the DELIMITER
CREATE TRIGGER pointsAssigns
BEFORE INSERT ON MyTable
FOR EACH ROW
SET NEW.points = IF(NEW.position > 6, 5, NEW.points);
Here is a SQLFiddle demo
Spent about one hour to understand.
Where is this sql trigger contain syntax error?
CREATE
TRIGGER playlis_trubric_count_on_playlist_shared_update
AFTER UPDATE ON playlist_playlist
FOR EACH ROW
IF (NEW.shared != OLD.shared) AND (NEW.shared = 1) THEN
UPDATE etv.playlist_playlistrubric
SET count = playlist_playlistrubric.count + 1
WHERE etv.playlist_playlistrubric.id = NEW.rubric_id;
ELSEIF (NEW.shared != OLD.shared) AND (NEW.shared = 0) THEN
UPDATE etv.playlist_playlistrubric
SET count = playlist_playlistrubric.count - 1
WHERE etv.playlist_playlistrubric.id = NEW.rubric_id
END IF;
ERROR SAYS:
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 'ELSEIF (NEW.shared != OLD.shared) AND (NEW.shared = 0) THEN
UPDATE etv.p' at line 1
From the documentation:
By using the BEGIN ... END construct, you can define a trigger that
executes multiple statements. Within the BEGIN block, you also can use
other syntax that is permitted within stored routines such as
conditionals and loops. 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. The following example illustrates these points. It defines
an UPDATE trigger that checks the new value to be used for updating
each row, and modifies the value to be within the range from 0 to 100.
This must be a BEFORE trigger because the value needs to be checked
before it is used to update the row:
mysql> delimiter //
mysql> CREATE TRIGGER upd_check BEFORE UPDATE ON account
-> FOR EACH ROW
-> BEGIN
-> IF NEW.amount < 0 THEN
-> SET NEW.amount = 0;
-> ELSEIF NEW.amount > 100 THEN
-> SET NEW.amount = 100;
-> END IF;
-> END;//
mysql> delimiter ;
You're missing BEGIN and END and you're also going to need the delimiter trick, because at present your trigger definition ends at the first NEW.rubric_id (which is why the parse error occurs just after that point).