mysql error with a trigger - mysql

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 ;

Related

How to create a BEFORE INSERT Trigger

I'm trying to create a BEFORE INSERT trigger and getting errors that I don't understand.
The MySQL to create the trigger looks like this:
CREATE TRIGGER `GetPolyLinkID` BEFORE INSERT ON `TableA`
FOR EACH ROW
BEGIN
INSERT INTO TableB () VALUES ();
NEW.PolyLinkID = last_insert_id();
END
The error I'm getting is
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 1
You forgot SET ... begore NEW.PolyLinkID = last_insert_id();
And
For creating complex trigger , you must use the delimiter keyword .
This is a example from rom https://dev.mysql.com/doc/refman/5.6/en/trigger-syntax.html .
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 ;
Screen shot with sequel pro

iPage MySql Create Or Replace Trigger

I'm having issues creating a trigger in MySql Database on iPage hosting.
Here is the code:
delimiter $$
CREATE TRIGGER update_rfp_estado BEFORE UPDATE ON wp_ac_rfp
FOR EACH ROW
BEGIN
IF DATEDIFF(CURDATE(),NEW.fecini) < 0 THEN
SET NEW.estado = 1;
ELSEIF DATEDIFF(CURDATE(),NEW.fecfin) > 0 THEN
SET NEW.estado = 3;
ELSEIF DATEDIFF(CURDATE(),NEW.fecini) >= 0 THEN
SET NEW.estado = 2;
END IF;
END;$$
delimiter;
This trigger checks the current date with the new date and change the NEW.estado depending on the conditionals.
The error i'm getting is:
#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 'delimiter $$
CREATE TRIGGER `update_rfp_estado` BEFORE UPDATE ON `wp_ac_rfp` ' at line 1
What could be the error? Thank you in advance

Getting 1064 error while creating mysql trigger

I am trying to create mysql trigger but getting an error
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 14
Here is my sql script
delimiter $$
CREATE TRIGGER `update_account_balance_after_transfer` AFTER INSERT ON `balance_transfer`
FOR EACH ROW
BEGIN
IF(NEW.from_account_type='CUSTOMER') THEN
UPDATE customer c SET c.balance = c.balance-NEW.amount WHERE c.customer_id= NEW.from_account_id;
ELSE IF(NEW.from_account_type='Vendor') THEN
UPDATE vendor V SET v.balance = v.balance-NEW.amount WHERE v.vendor= NEW.from_account_id;
END IF;
IF(NEW.to_account_type='VENDOR') THEN
UPDATE customer c SET c.balance = c.balance+NEW.amount WHERE c.customer_id= NEW.to_account_id;
ELSE IF(NEW.to_account_type='Vendor') THEN
UPDATE vendor V SET v.balance = v.balance+NEW.amount WHERE v.vendor= NEW.to_account_id;
END IF;
END $$
delimiter ;
Remove space between ELSE and IF. It should be ELSEIF as below.
delimiter $$
CREATE TRIGGER `update_account_balance_after_transfer` AFTER INSERT ON `balance_transfer`
FOR EACH ROW
BEGIN
IF(NEW.from_account_type='CUSTOMER') THEN
UPDATE customer c SET c.balance = c.balance-NEW.amount WHERE c.customer_id= NEW.from_account_id;
ELSEIF(NEW.from_account_type='Vendor') THEN
UPDATE vendor V SET v.balance = v.balance-NEW.amount WHERE v.vendor= NEW.from_account_id;
END IF;
IF(NEW.to_account_type='VENDOR') THEN
UPDATE customer c SET c.balance = c.balance+NEW.amount WHERE c.customer_id= NEW.to_account_id;
ELSEIF(NEW.to_account_type='Vendor') THEN
UPDATE vendor V SET v.balance = v.balance+NEW.amount WHERE v.vendor= NEW.to_account_id;
END IF;
END $$
delimiter ;
MySQL IF Syntax
Check the demo here

Mysql-Trigger syntax error

I'm trying to create a trigger on a table, but I keep getting an error. Any idea what's wrong with the following statement?
CREATE TRIGGER `some_name` BEFORE UPDATE ON `some_table`
FOR EACH ROW BEGIN
IF NEW.isDeleted = 1 THEN
SET NEW.isSearchable = 0;
ELSE THEN
SET NEW.isSearchable = 1;
END IF;
END;
Mysql output:
#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
You are missing delimiter and also no need of then after else
delimiter //
create trigger `some_name` BEFORE UPDATE ON `some_table`
for each row
begin
if new.isDeleted = 1 then
SET NEW.isSearchable = 0;
else
SET NEW.isSearchable = 1;
end if;
end;//
delimiter ;

MySQL Trigger Syntax Error

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;