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
Related
I am trying to create a mysql trigger. This is what I am trying to create, but somehow it tells me that: "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 'A
FOR EACH ROW".
What Am I doing wrong here? I have tried to naming them differently, but it does not seem to work.
delimiter //
CREATE TRIGGER stock_update BEFORE UPDATE ON `stock` A
FOR EACH ROW
BEGIN
IF NEW.`quantity` < 1 THEN //belongs to alias A
UPDATE `ps_product` B
SET B.`visibility` = 'none';
WHERE id_product = OLD.id_product //Should be the id_product of A alias
ELSEIF NEW.quantity > 0 THEN
UPDATE `ps_product` C
SET C.`visibility` = 'both';
WHERE id_product = OLD.id_product //Should be the id_product of A alias
END IF;
END;//
delimiter ;
Try this one
delimiter //
CREATE TRIGGER stock_update BEFORE UPDATE ON `stock`
FOR EACH ROW
BEGIN
IF NEW.`quantity` < 1 THEN #belongs to alias A
UPDATE `ps_product` B
SET B.`visibility` = 'none'
WHERE id_product = OLD.id_product; #Should be the id_product of A alias
ELSEIF NEW.quantity > 0 THEN
UPDATE `ps_product` C
SET C.`visibility` = 'both'
WHERE id_product = OLD.id_product; #//Should be the id_product of A alias
END IF;
END;//
delimiter ;
This shows no errors in mysql workbbench
DELIMITER $$
DROP TRIGGER IF EXISTS TRIGGER_BEFORE_USERS_MAUALEXPIRY_USEREXPIRAY_UPDATE $$
CREATE TRIGGER TRIGGER_BEFORE_USERS_MAUALEXPIRY_USEREXPIRAY_UPDATE
BEFORE UPDATE ON USERS
FOR EACH ROW
BEGIN
IF #DISABLE_TRIGGERS IS NULL THEN
IF NOT (NEW.MANUALEXPIRATIONDATE <=> OLD.MANUALEXPIRATIONDATE) THEN
SET NEW.CUSTOMFIELD2 = NEW.MANUALEXPIRATIONDATE;
IF NOT(NEW.USEREXPIRYDATE <=> OLD.USEREXPIRYDATE) THEN
SET NEW.CUSTOMFIELD3 = NEW.USEREXPIRYDATE;
END IF;
END IF;
END$$
DELIMITER ;
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 '' at line 17
CREATE TRIGGER TRIGGER_BEFORE_USERS_MAUALEXPIRY_USEREXPIRAY_UPDATE
BEFORE UPDATE ON USERS
FOR EACH ROW
BEGIN
IF #DISABLE_TRIGGERS IS NULL THEN
IF NOT (NEW.MANUALEXPIRATIONDATE <=> OLD.MANUALEXPIRATIONDATE) THEN
SET NEW.CUSTOMFIELD2 = NEW.MANUALEXPIRATIONDATE;
END IF;
IF NOT(NEW.USEREXPIRYDATE <=> OLD.USEREXPIRYDATE) THEN
SET NEW.CUSTOMFIELD3 = NEW.USEREXPIRYDATE;
END IF;
END IF;
END
$$
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 ;
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 have some problems trying to do a trigger.
Here is my MySQL query for the trigger:
delimiter //
CREATE TRIGGER `aggiornaProduzione`
BEFORE UPDATE ON `strutture` FOR EACH ROW
BEGIN
DECLARE temp bigint;
IF ( tipo=1/*mercato*/ AND old.livello <> new.livello )
THEN (
SELECT round(2*livello*livello + 13.8*livello)
FROM strutture WHERE tipo=1 AND city=old.city INTO temp;
WHILE (temp%6<>0) temp=temp+1;
END WHILE;
UPDATE strutture SET produzione=temp WHERE city=new.city AND tipo=1;
)
END IF;
END //
And the error 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 '; WHILE (temp%6<>0) temp=temp+1; END WHILE; UPDATE strutture SET ' at line 8
Does someone have an idea why I'm getting this error?
Remove the parentheses around your THEN clause. Also your WHILE clause is syntactically incorrect:
delimiter //
CREATE TRIGGER `aggiornaProduzione`
BEFORE UPDATE ON `strutture` FOR EACH ROW
BEGIN
DECLARE temp bigint;
IF ( tipo=1/*mercato*/ AND old.livello <> new.livello )
THEN
SELECT round(2*livello*livello + 13.8*livello)
FROM strutture WHERE tipo=1 AND city=old.city INTO temp;
WHILE temp%6<>0 DO
SET temp=temp+1;
END WHILE;
UPDATE strutture SET produzione=temp WHERE city=new.city AND tipo=1;
END IF;
END //