MySQL IF statement in Trigger - mysql

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 ;

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 ;

I am trying to learn triggers on my sql and getting the following errors

This is the code I am executing
create trigger salarydifff
after update on office
for each row
declare sal_diff number
begin
sal_diff := :new.salary- :old.salary
end
and I am getting 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 'declare sal_diff number begin sal_diff := :new.salary- :old.salary end' at line 4
Your code looks like Oracle not mysql
Mysql would look like this
drop trigger if exists salarydiff;
delimiter //
create trigger salarydifff
after update on office
for each row
begin
declare sal_diff integer ;
set sal_diff = new.amt - old.amt ;
end //
delimiter ;

#1064 - You have an error in your SQL syntax when create trigger

I have faced this problem when I created an after insert trigger. I know this is an syntax error, but I don't know how to fix it in my 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 '' at line 5
Here is my trigger:
CREATE TRIGGER insert_to_student_table
AFTER INSERT ON user
FOR EACH ROW
BEGIN
DECLARE v_UserID INTEGER;
SET #v_UserID := (Select Max(UserID) from user where Type="Student");
INSERT INTO student (StudentID,TutorID)VALUES (v_UserID, NULL);
END
You need to have a delimiter
delimiter //
CREATE TRIGGER insert_to_student_table
AFTER INSERT ON user
FOR EACH ROW
BEGIN
DECLARE v_UserID INTEGER;
SET v_UserID = (Select Max(UserID) from user where Type="Student");
INSERT INTO student (StudentID,TutorID)VALUES (v_UserID, NULL);
END;//
delimiter ;

Error 1064 mysql syntax

I am new to Mysql and I am trying to write mysql stored procedure with transaction.but I am getting 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 21
Here is my procedure:
DELIMITER $$
CREATE PROCEDURE InsertVote(IN image_param VARCHAR(30),
IN user_id_param INT,
IN vore_char CHAR(1)
)
Begin
START TRANSACTION;
IF vote_char="A" THEN
UPDATE img_options SET A_result=(A_result+1) WHERE image=image_param;
ELSE IF vote_char="B" THEN
UPDATE img_options SET B_result=(B_result+1) WHERE image=image_param;
ELSE IF vote_char="C" THEN
UPDATE img_options SET C_result=(C_result+1) WHERE image=image_param;
ELSE
UPDATE img_options SET D_result=(D_result+1) WHERE image=image_param;
END IF;
Insert into WATCH_FEED_EVEN values(image_param,user_id_param);
select * from img_options where image=image_param;
COMMIT; <---Line No 21
END $$
DELIMITER ;
I tried to solve this one for more than 2 hours but I couldn't able to do this.Can anyone help me?

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 ;