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).
Related
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
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.
I try to create trigger before table is updated. I want to it works when two specific columns are changed: status and sale_profit columns. If one of these columns is updated, trigger must set is_changed column's value to 1. But the below code gives me error like this:
Error:
MySQL Error 1064: You have an error in your SQL syntax ... syntax use near at line 7
SQL code:
CREATE TRIGGER updateIsChanged
BEFORE UPDATE ON manage_product
FOR EACH ROW
BEGIN
IF NEW.`status` <> OLD.`status` || NEW.sale_profit <> OLD.sale_profit
then
SET NEW.is_changed = 1;
END IF;
END
Ok. I solved my problem by adding delimiter to beginning and end of the code.
UPDATED CODE:
delimiter $$
CREATE TRIGGER updateIsChanged
BEFORE UPDATE ON manage_product
FOR EACH ROW
BEGIN
IF NEW.`status` <> OLD.`status` || NEW.sale_profit <> OLD.sale_profit THEN
SET NEW.is_changed = 1;
END IF;
END$$
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
How do I realize the following in MySQL with Triggers:
When value of some column is null -> set other column values to null
and
when value of some column is not null -> set other column values to null
table definition:
CREATE TABLE variations (
id int(10) NOT NULL,
x1 int(10) NOT NULL,
x2 int(10),
x1_option1 BOOL,
x2_option1 BOOL,
x1_option2 varchar(10),
x2_option2 varchar(10)
);
The idea is that we have 2 Elements, x1and x2. While x1is mandatory, x2is optional and can be null. Both, x1 and x2 have two options: x1_option1 , x2_option1, x1_option2 and x2_option2.
The first rule should be that when x2 is null, both options for x2 (x2_option1, x2_option2) must also be null.
My attempt:
CREATE
TRIGGER check_null_x2 BEFORE INSERT
ON variations
FOR EACH ROW BEGIN
IF NEW.x2 IS NULL THEN
SET NEW.x2_option1 = NULL;
SET NEW.x2_option2 = NULL;
END IF;
END$$
Throws 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
Can you please help me figuring out whats wrong? I just dont understand what '' means.
The second rule should be that there can only be one of the two options selected. that means if x2_option1 is NOT NULL, x2_options2 must be NULL. In general i think this can be done the same way as the first rule. My question: how can i do multiple 'IF', 'ELSE IF' etc in one trigger?
This is syntax for trigger:
delimiter //
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;//
delimiter ;
...and your code is here:
DELIMITER //
CREATE TRIGGER check_null_x2 BEFORE INSERT ON variations
FOR EACH ROW
BEGIN
IF NEW.x2 IS NULL THEN
SET NEW.x2_option1 = NULL;
SET NEW.x2_option2 = NULL;
END IF;
END$$ -- THIS LINE SHOULD BE: "END;//"
DELIMITER ;
EDIT:
The official Documentation says the following:
If you use the mysql client program to define a stored program containing semicolon characters, a problem arises. By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server.
To redefine the mysql delimiter, use the delimiter command. The following example shows how to do this for the dorepeat() procedure just shown. The delimiter is changed to // to enable the entire definition to be passed to the server as a single statement, and then restored to ; before invoking the procedure. This enables the ; delimiter used in the procedure body to be passed through to the server rather than being interpreted by mysql itself.
you seem to have ";" set as DELIMETER, which causes the query to execute once it sees a ";". try changing it first:
DELIMITER //
CREATE
TRIGGER check_null_x2 BEFORE INSERT
ON variations
FOR EACH ROW BEGIN
IF NEW.x2 IS NULL THEN
SET NEW.x2_option1 = NULL;
SET NEW.x2_option2 = NULL;
END IF;
END;//
DELIMITER ;
CREATE TRIGGER `XXXXXX` BEFORE INSERT ON `XXXXXXXXXXX` FOR EACH ROW BEGIN
IF ( NEW.aaaaaa IS NULL ) THEN
SET NEW.XXXXXX = NULL;
SET NEW.YYYYYYYYY = NULL;
END IF;
END
Worked for me....