Mysql-Trigger syntax error - mysql

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 ;

Related

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

MySQL IF statement in Trigger

I just want to create trigger in my sql but some error happened
this is the code
CREATE TRIGGER delete_santri_in_kamar
AFTER UPDATE ON
santri
FOR EACH ROW
BEGIN
DECLARE stat INT
SET stat = select status FROM santri WHERE id_santri=new.id_santri
IF (stat = 0) THEN
DELETE FROM santri_kamar_asrama WHERE id_santri=new.id_santri
END IF
END
and this is the error message
#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 'SET stat = select status FROM santri WHERE id_santri=new.id_santri
IF (stat =' at line 7
please help me
Add semicolons after the statements and change default delimiter(;) before the code of create trigger. Otherwise it will give
SQL Error(1064): You have an error in your SQL Syntax;
After the create trigger code make ; as default delimiter
DELIMITER $$
CREATE TRIGGER delete_santri_in_kamar
AFTER UPDATE ON
santri
FOR EACH ROW
BEGIN
DECLARE stat INT;
SET stat = (select status FROM santri WHERE id_santri=new.id_santri);
IF (stat = 0) THEN
DELETE FROM santri_kamar_asrama WHERE id_santri=new.id_santri;
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: error when checking if variable exists

I have a trigger which references a global variable that's coming from a delete query which sets the trigger off:
DELIMITER ;;
CREATE TRIGGER test_trigger BEFORE DELETE ON test_table FOR EACH ROW
BEGIN
IF (SELECT #userID IS NULL)
THEN #userID := 0
END IF;
END
;;
DELIMITER ;
For some reason I'm getting SQL error:
SQL 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 '#userID := 0
I don't understand this, it's a very basic IF statement, why doesn't it work?
UPDATE: I'm totally confused, I've tried this
IF TRUE
THEN TRUE
END IF;
and it still throws an error... Seriously?
UPDATE 2: SOLVED This actually works, however it is very weird
IF (SELECT #userID IS NULL)
THEN SET userID = 0;
END IF;
Apparently you need a semicolon inside IF statement?
CREATE TRIGGER test_trigger BEFORE DELETE ON test_table FOR EACH ROW
BEGIN
IF (SELECT #userID IS NULL)
THEN
SET #userID = 0;
END IF;
END;

Mysql create trigger 1064 error

The sql i wrote
**delimiter |
CREATE
DEFINER=CURRENT_USER
TRIGGER set_profiletype_after_insert BEFORE INSERT ON trl_translator FOR EACH ROW
BEGIN
UPDATE trl_profile SET trl_profile.type = 'translator' WHERE trl_profile.profile_id = NEW.translator_id
END
|
delimiter ;**
The error given
[SQL]
**CREATE
DEFINER=CURRENT_USER
TRIGGER set_profiletype_after_insert BEFORE INSERT ON trl_translator FOR EACH ROW
BEGIN
UPDATE trl_profile SET trl_profile.type = 'translator' WHERE trl_profile.profile_id = NEW.translator_id
END
;
[Err] 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 6**
How to solve this ? where is the mistake i do ?
It looks like you simply need to terminate your UPDATE statement with a semicolon.
Try this code
delimiter |
CREATE
DEFINER=CURRENT_USER
TRIGGER set_profiletype_after_insert BEFORE INSERT ON trl_translator FOR EACH ROW
BEGIN
UPDATE trl_profile SET trl_profile.type = 'translator' WHERE trl_profile.profile_id = NEW.translator_id;
END;|
delimiter ;