Facing Error while creating a MYSQL trigger - mysql

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
$$

Related

I am getting a syntax error when creating a MySQL trigger

I try to create a trigger but I get this error after executing the SQL:
# 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 'DECLARE temp INT' at line 4
Here is my trigger:
CREATE TRIGGER hesap
AFTER INSERT ON uber
DECLARE temp INT;
BEGIN IF uber.il='ANKARA' THEN
temp=5+(uber.mesafe*0.5);
ELSEIF uber.il='ISTANBUL' THEN
temp=10+(uber.mesafe*0.5);
ELSEIF uber.il='IZMIR' THEN
temp=3+(uber.mesafe*0.5);
END IF;
INSERT INTO fatura VALUES(uber.uid,temp)
END;
There is some syntax error as I identified check out the below code:
delimiter $$
CREATE TRIGGER hesap
AFTER INSERT ON uber FOR EACH ROW
BEGIN
DECLARE temp integer;
IF new.il = 'ANKARA' THEN
temp= 5+(uber.mesafe*0.5);
ELSEIF new.il='ISTANBUL' THEN
temp=10+(uber.mesafe*0.5);
ELSEIF new.il='IZMIR' THEN
temp=3+(uber.mesafe*0.5);
END IF;
INSERT INTO fatura VALUES(new.uid,temp);
END
delimiter ;

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 error with a 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 ;

MYSQL Trigger syntax issue

I am trying this trigger but it keeps giving me this error:
Trigger
DELIMITER $$
CREATE
TRIGGER entries_limit_trigger
BEFORE INSERT ON Feed
FOR EACH ROW
BEGIN
Set #counts=(SELECT count(*) from Feed);
IF (#counts > 10000)
THEN
PRINT 'INSERTED SUCCESS';
END IF;
END
$$
DELIMITER ;
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 ''INSERTED SUCCESS'; END IF; END' at line 9
If you want the trigger to exit with a message you can do:
CREATE
TRIGGER entries_limit_trigger
BEFORE INSERT ON Feed
FOR EACH ROW
BEGIN
Set #counts=(SELECT count(*) from Feed);
IF #counts > 10000 THEN
set #msg = "Reached the limit";
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = #msg;
END IF;
END;
sqlfiddle demo
To make it exit with a message, just add another insert to the fiddle.