I have two tables, t1 and t2.
I'm trying to make a trigger so that when t1 gets an update, I check if what was updated was t1.nStatus.
If t1.nStatus = 2, I need to set t2.bEnabled = 1 for all t2.customer_Id that are equal to t1.nId
CREATE OR REPLACE TRIGGER change AFTER UPDATE ON t1
FOR EACH ROW
BEGIN
IF NEW.nStatus = 2 THEN
UPDATE t2 SET bEnabled = 1 WHERE t2.immobile_id = NEW.nId;
END IF;
END;
The error I got:
1 queries executed, 0 success, 1 errors, 0 warnings
Query: CREATE OR REPLACE trigger changeStatusImmobile after UPDATE on
immobile for each row begin IF NEW.nStatus = 2 then UPDATE select...
Error Code: 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 '' at line 5
Execution Time : 0 sec Transfer Time : 0 sec Total Time : 0.146
sec
Have you some idea?
Try setting a delimiter
DELIMITER $$
CREATE OR REPLACE TRIGGER change AFTER UPDATE ON t1
FOR EACH ROW
BEGIN
IF NEW.nStatus = 2 THEN
UPDATE t2 SET bEnabled = 1 WHERE t2.immobile_id = NEW.nId;
END IF;
END$$
DELIMITER ;
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
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 want to create a trigger which will be executed when a new row is about to be inserted if my condition is satisfied ( latest version = 1 ) all previous row will be updated with latest version = 0
CREATE TRIGGER remiseazero
BEFORE INSERT
ON wp_stattype2_3_activite
FOR EACH ROW
BEGIN
IF NEW.latestversion = 1
THEN
update wp_stattype2_3_activite
set latestversion = 0 where typecas = new.typecas;
END IF;
END;$$
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 9
Put delimiter. And remove ; after END.
DELIMITER $$
CREATE TRIGGER remiseazero
BEFORE INSERT
ON wp_stattype2_3_activite
FOR EACH ROW
BEGIN
IF (NEW.latestversion = 1) THEN
update wp_stattype2_3_activite set latestversion = 0 where typecas = NEW.typecas;
END IF;
END$$
DELIMITER ;
I want to add trigger in mysql ...
CREATE TRIGGER mytrigger BEFORE INSERT ON table_1
FOR EACH ROW
BEGIN SET
NEW.`order` = (SELECT 1 + COALESCE((SELECT MAX(`order`) FROM `table_1`), 0));
END
And I always get error:
Error
SQL query: Documentation
CREATE TRIGGER mytrigger BEFORE INSERT ON table_1
FOR EACH
ROW
BEGIN SET NEW.`order` = ( SELECT 1 + COALESCE( (
SELECT MAX( `order` )
FROM `table_1` ) , 0 )
);
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 '' at line 4
Any ideas?
You need to change the delimiter. Otherwise the engine thinks the statement is finished at the first ;. But that would be wrong.
delimiter |
CREATE TRIGGER mytrigger BEFORE INSERT ON table_1
FOR EACH ROW
BEGIN
SET NEW.`order` = (SELECT 1 + COALESCE((SELECT MAX(`order`) FROM `table_1`), 0));
END
|
delimiter ;
Running on MySQL 5.5.9 with InnoDB.
I created the following trigger:
CREATE TRIGGER TRIGGER_Products_Insert
AFTER INSERT ON Products
FOR EACH ROW
BEGIN
UPDATE Products
SET current = 0
WHERE id = new.id
AND current = 1
AND autonumber <> new.autonumber
END;
MySQLWorkbench shows a syntax error on the last line, and executing this throws the following error
Error Code: 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 11
Where is my error?
http://dev.mysql.com/doc/refman/5.5/en/create-trigger.html
DELIMITER |
CREATE TRIGGER TRIGGER_Products_Insert AFTER INSERT ON Products
FOR EACH ROW BEGIN
UPDATE Products
SET current = 0
WHERE id = new.id
AND current = 1
AND autonumber <> new.autonumber;
END;
|
DELIMITER ;
You are missing a ; before your END keyword:
CREATE TRIGGER TRIGGER_Products_Insert
AFTER INSERT ON Products
FOR EACH ROW
BEGIN
UPDATE Products
SET current = 0
WHERE id = new.id
AND current = 1
AND autonumber <> new.autonumber;
END;