Syntax in mysql trigger - mysql

I am writing a trigger on mysql table. The code is following
DELIMITER $$
CREATE TRIGGER update_on_product_option after update on db_name.table_name
FOR each row
BEGIN
if new.isactive <> old.isactive then
SET #inc=1;
-- update statement on some table here
else if new.lastupdatedts <> old.lastupdatedts then
-- update statement on some table here
set #t = 0;
end if;
end if;
END;
$$
DELIMITER ;
As you can see there are two end if at the end and surprisingly this trigger function is not throwing any errors even though there is an extra end if; at the end. If I remove one end if, then it throws an error. unable to understand why this is happening.

Instead of ELSE IF, MySQL's syntax uses ELSEIF (without the space).
https://dev.mysql.com/doc/refman/5.7/en/if.html
DELIMITER $$
CREATE TRIGGER update_on_product_option after update on db_name.table_name
FOR each row
BEGIN
if new.isactive <> old.isactive then
SET #inc=1;
-- update statement on some table here
elseif new.lastupdatedts <> old.lastupdatedts then
-- update statement on some table here
set #t = 0;
end if;
END;
$$
DELIMITER ;
Although you might be able to make it work with the space in ELSE IF by adding an additional END IF. By using the space, you effectively initiate a second IF statement, which must be closed independently of the first outer IF statement.

This is completely normal(nested two IF stamements so both IF have to be closed):
if new.isactive <> old.isactive then
SET #inc=1;
else if new.lastupdatedts <> old.lastupdatedts then
set #t = 0;
end if;
end if;
You probably wanted:
if new.isactive <> old.isactive then
SET #inc=1;
elseif new.lastupdatedts <> old.lastupdatedts then
SET #t = 0;
end if;
The difference is ELSE IF vs ELSEIF.

Use elseif (without space ) instead of else if. The reasion is when you use else if with space its consider else is use with starting if and you create new condition with if so its required to close with end if. So in your query required two end if.
DELIMITER $$ CREATE TRIGGER update_on_product_option after update on db_name.table_name FOR each row BEGIN if new.isactive <> old.isactive then SET #inc=1; -- update statement on some table here elseif new.lastupdatedts <> old.lastupdatedts then -- update statement on some table here set #t = 0; end if; END; $$ DELIMITER ;

Related

What is wrong with the 'when' clause?

CREATE TRIGGER UPDATE_CAR_DETALS
AFTER UPDATE ON BOOKING_DETALS
FOR EACH ROW
WHEN (IFNULL(TO_CHAR(NEW.ACT_RET_DT_TIME),NULL) <> 'NULL' OR
NEW.BOOKING_STATUS ='C')
Multi statements in triggers need a BEGIN and END
Also when you use query tabs you need also DELIMITER
CASE WHEN you would use if you have multiple choices, but you use better IF THEN
DELIMITER $$
CREATE TRIGGER UPDATE_CAR_DETALS
AFTER UPDATE ON BOOKING_DETALS
FOR EACH ROW
BEGIN
IF (IFNULL(TO_CHAR(NEW.ACT_RET_DT_TIME),NULL) <> 'NULL' OR NEW.BOOKING_STATUS ='C') then
BEGIN
IF NEW.BOOKING_STATUS ='C' THEN
UPDATE CAR SET AVAILABILITY_FLAG = 'A' , LOC_ID = NEW.PICKUP_LOC
WHERE REGISTRATION_NUMBER = NEW.REG_NUM;
ELSE
IF IFNULL(TO_CHAR(NEW.ACT_RET_DT_TIME),NULL) <> NULL THEN
UPDATE CAR SET AVAILABILITY_FLAG = 'A' , LOC_ID = NEW.DROP_LOC, MILEAGE = MILEAGE+GET_MILEAGE
WHERE REGISTRATION_NUMBER = NEW.REG_NUM;
END IF;
END IF;
END;
END IF;
END;
DELIMITER ;

Can i use nested BEGIN/END blocks in Triggers (mySQL)

I would like to know why my code is not working. The ideia is to verify that when a idseq is added to a certain table, to see if that same idseq already exists in any other table.
The following code produces ERROR 1235 in mySQL:
delimiter //
CREATE TRIGGER trigger_pagina
BEFORE INSERT ON pagina
FOR EACH ROW
BEGIN
IF (EXISTS(
SELECT R.idseq
FROM registo R
WHERE (NEW.idseq = R.idseq)
) )THEN
CALL Ilegal_Insert();
END IF;
END;
BEGIN
IF (EXISTS(
SELECT T.idseq
FROM tipo_registo T
WHERE (NEW.idseq = T.idseq)
)) THEN
CALL Ilegal_Insert();
END IF;
END;
BEGIN
IF (EXISTS(
SELECT V.idseq
FROM valor V
WHERE (NEW.idseq = V.idseq)
)) THEN
CALL Ilegal_Insert();
END IF;
END;
BEGIN
IF (EXISTS(
SELECT C.idseq
FROM campo C
WHERE (NEW.idseq = C.idseq)
)) THEN
CALL Ilegal_Insert();
END IF;
END;//
delimiter ;
Thanks in advance !
You need another, outermost BEGIN ... END block to enclose the ones you have above, like
delimiter //
CREATE TRIGGER trigger_pagina
BEFORE INSERT ON pagina
FOR EACH ROW
BEGIN -- Add a BEGIN here...
BEGIN
IF (EXISTS(
...
END IF;
END;
BEGIN
...
END;
END; -- and an END here
//
delimiter ;
This is because CREATE TRIGGER actually only allows the trigger body to contain a single statement, and BEGIN ... END is treated, along with its contents, as a single statement.
So yes, you can (and in this case must) use nested BEGIN ... END blocks in a trigger body.

Update one mysql column when another is edited

I'm trying to make a datetime field that automatically gets updated with the current time only if there was a change to a certain field.
It seems I have a syntax error.
CREATE OR ALTER TRIGGER last_progress_date
ON wp_task_mgr
AFTER UPDATE
AS BEGIN
IF UPDATE (progress_percentage)
SET last_progress_date = GETDATE()
END
Just for future reference, I found the answer here:
https://dba.stackexchange.com/questions/125203/simple-trigger-to-update-two-columns-in-one-table
MySQL query:
DELIMITER //
CREATE TRIGGER trig_1 before insert
ON <table_name> FOR EACH ROW
BEGIN
IF new.due_date is not null and new.end_date='' then
set new.end_date=new.due_date;
end if;
IF new.end_date is not null and new.due_date='' then
set new.due_date=new.end_date;
end if;
END;
//
DELIMITER //
CREATE TRIGGER trig_2 before update
ON <table_name> FOR EACH ROW
BEGIN
IF new.due_date <>old.due_date then
set new.end_date=new.due_date;
end if;
IF new.end_date <> old.end_date then
set new.due_date=new.end_date;
end if;
END;
//

Trigger problems in mysql

i have a table named reservation which consists of column reservationStatus and place and another table spot which consists of spot_id and spot_status.
i have created a trigger in reservation table which should activate after the reservationStatus has been set to 0. but it is not working
DELIMITER $$
CREATE TRIGGER spotCancel
AFTER UPDATE ON reservation
FOR EACH ROW
BEGIN
IF NEW.reservationStatus = 0 AND OLD.place <=> 'AbcMall' THEN
UPDATE spot SET spot_status = 0 WHERE spot_id <=> OLD.spot_id;
END IF;
IF NEW.reservationStatus = 0 AND OLD.place <=> 'XyzGym' THEN
UPDATE spot1 SET spot_status =0 WHERE spot_id <=> OLD.spot_id;
END IF;
END$$
DELIMITER ;
This query fails silently everytime and i couldnot find the error. I am using xampp and mysql innoDB storage engine.
You're missing an END IF.
DELIMITER $$
CREATE TRIGGER spotCancel
AFTER UPDATE ON reservation
FOR EACH ROW
BEGIN
IF NEW.reservationStatus = 0 AND OLD.place <=> 'Abc' THEN
UPDATE spot SET spot_status = 0 WHERE spot_id <=> OLD.spot_id;
END IF;
IF NEW.reservationStatus = 0 AND OLD.place <=> 'Def' THEN
UPDATE spot1 SET spot_status =0 WHERE spot_id <=> OLD.spot_id;
END IF;
END$$
DELIMITER ;

MySQL trigger if condition exists

I'm trying to write an update trigger that will only update a password when a new password is set in the update statement but I'm having a terrible time trying to nail down the syntax. This should be a no-brainer but I'm just not finding the solution.
Here's my code:
CREATE TRIGGER upd_user BEFORE UPDATE ON `user`
FOR EACH ROW BEGIN
IF (NEW.password <> '') THEN
SET NEW.password = PASSWORD(NEW.password);
END IF;
END;
I've tried:
IF (NEW.password <> NULL) THEN
IF (NEW.password) THEN
IF NEW.password <> NULL THEN
IF (NEW.password > 0) THEN
IF (NEW.password != NULL) THEN
And I'm sure many other combinations but it's just not working. Does anyone have any insight?
I think you mean to update it back to the OLD password, when the NEW one is not supplied.
DROP TRIGGER IF EXISTS upd_user;
DELIMITER $$
CREATE TRIGGER upd_user BEFORE UPDATE ON `user`
FOR EACH ROW BEGIN
IF (NEW.password IS NULL OR NEW.password = '') THEN
SET NEW.password = OLD.password;
ELSE
SET NEW.password = Password(NEW.Password);
END IF;
END$$
DELIMITER ;
However, this means a user can never blank out a password.
If the password field (already encrypted) is being sent back in the update to mySQL, then it will not be null or blank, and MySQL will attempt to redo the Password() function on it. To detect this, use this code instead
DELIMITER $$
CREATE TRIGGER upd_user BEFORE UPDATE ON `user`
FOR EACH ROW BEGIN
IF (NEW.password IS NULL OR NEW.password = '' OR NEW.password = OLD.password) THEN
SET NEW.password = OLD.password;
ELSE
SET NEW.password = Password(NEW.Password);
END IF;
END$$
DELIMITER ;
Try to do...
DELIMITER $$
CREATE TRIGGER aumentarsalario
BEFORE INSERT
ON empregados
FOR EACH ROW
BEGIN
if (NEW.SALARIO < 900) THEN
set NEW.SALARIO = NEW.SALARIO + (NEW.SALARIO * 0.1);
END IF;
END $$
DELIMITER ;