What is wrong with the 'when' clause? - mysql

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 ;

Related

Mysql trigger to update empty field with condition

I have a MySQL table , look like this
t id lang title
1 7 en_UK my_title
1 7 kh_KH
I want write a trigger that update title to my_title with the same id is 7
Result
t id lang title
1 7 en_UK my_title
1 7 kh_KH my_title
From my understanding.
DELIMITER $$
CREATE TRIGGER upd_title BEFORE UPDATE ON `term`
FOR EACH ROW BEGIN
IF (NEW.title IS NULL OR NEW.title= '') THEN
SET NEW.title= ??? ;
END IF;
END$$
DELIMITER ;
[UPDATE1]->not works (trigger not being created)
DELIMITER $$
DROP TRIGGER IF EXISTS `update_category_after_insert`
CREATE TRIGGER `update_category` AFTER INSERT ON `categories`
FOR EACH ROW BEGIN
DECLARE loc_title text;
IF (NEW.libelle_categorie IS NULL OR NEW.libelle_categorie= '') THEN
select libelle_categorie into loc_title from categories where NEW.num_noeud= num_noeud and langue = 'en_UK';
SET NEW.libelle_categorie = loc_title;
END IF;
END
DELIMITER ;
[UPDATE2]
DELIMITER $$
CREATE TRIGGER ``update_category_after_insert`` BEFORE INSERT ON `categories`
FOR EACH ROW BEGIN
DECLARE loc_title text;
IF (NEW.libelle_categorie IS NULL OR NEW.libelle_categorie= '') THEN
select libelle_categorie into loc_title from categories where NEW.num_noeud= num_noeud and langue = 'en_UK';
SET NEW.libelle_categorie = loc_title;
END IF;
END
DELIMITER ;
Finally , I found the good solution for my case
UPDATE categories c INNER JOIN categories c2 ON (
c.num_noeud = c2.num_noeud
) SET c.libelle_categorie = c2.`libelle_categorie`
Can you clarify?
Are you trying to pull the value from the title column in the 'en_UK' row that exists when you insert an new row with the same id that HAS the title column not entered?
okay
CREATE TRIGGER upd_title BEFORE UPDATE ON `term`
FOR EACH ROW BEGIN
DECLARE loc_title VARCHAR(20);
IF (NEW.title IS NULL OR NEW.title= '') THEN
select title into loc_title from term where NEW.id = id and lang = 'en_UK';
SET NEW.title= loc_title;
END IF;
END
This should do the trick.
This was my trigger def:
CREATE DEFINER = CURRENT_USER TRIGGER `therinks`.`glreturndata_BEFORE_UPDATE` BEFORE UPDATE ON `glreturndata` FOR EACH ROW
BEGIN
DECLARE myVal VARCHAR(20);
if NEW.DESCRIPTION IS NULL or new.description = '' THEN
SELECT min(description) into myVal from glreturndata where category = NEW.category and new.idglreturndata <> idglreturndata;
Set NEW.DESCRIPTION = myval;
end if;
END
It seems that you can't do all this in a trigger. According to the documentation:
Within a stored function or trigger, it is not permitted to modify a table that is already being used (for reading or writing) by the statement that invoked the function or trigger.
According to this answer, it seems that you should:
create a stored procedure, that inserts into/Updates the target table, then updates the other row(s), all in a transaction.
With a stored proc you'll manually commit the changes (insert and update). I haven't done this in MySQL, but this post looks like a good example.

Syntax in mysql trigger

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 ;

Unable to execute "IF EXISTS" query

I am trying to run a SQL query, but something is messed up and I can't get below query to check if the data exists or not in the specified table.
The code I have is:
IF EXISTS(SELECT brth_day FROM sms.birthdaycheck WHERE brth_day = '2015-02-10')
THEN
BEGIN
UPDATE sms.birthdaycheck SET cnt = 1 WHERE brth_day = '2015-02-10'
END
ELSE
BEGIN
INSERT INTO sms.birthdaycheck(cnt,brth_day) VALUES (1,'2015-02-10')
END
END IF;
Edited solution :
delimiter ;;
create procedure XYZ(...parameters...)
begin
if exists(
SELECT brth_day FROM sms.birthdaycheck WHERE brth_day = '2015-
02-10')
then
UPDATE sms.birthdaycheck SET cnt = 1 WHERE brth_day = '2015-02-10';
else
INSERT INTO sms.birthdaycheck(cnt,brth_day) VALUES (1,'2015-02-10');
end if;
end;;
In MySQL, if control block must used inside a function or stored procedure.
So you need to rewrite your query:

Limit mysql insert value by trigger

I am trying to create a trigger to prevent empty string insert for cname column and values smaller than 7000 and larger than 8000 for empno column by making it null. Here is how I have done it:
delimiter $$
CREATE TRIGGER test1
BEFORE insert
ON clients
FOR EACH row BEGIN
if new.cname = '' THEN
SET new.cname = null;
if new.empno <7000 THEN
SET new.empno = null;
if new.empno>8000 THEN
SET new.empno = null;
end if;
end if;
end if;
end;
The cname column works fine. But the empno will accept anything and I cannot figure why. My table is something like this:
CREATE TABLE clients
(
empno INTEGER NOT NULL DEFAULT 7654
cname VARCHAR(20) NOT NULL
);
You should set each end if after the corresponding if:
delimiter $$
CREATE TRIGGER test1
BEFORE insert
ON clients
FOR EACH row BEGIN
if new.cname = '' THEN
SET new.cname = null;
end if;
if new.empno <7000 THEN
SET new.empno = null;
end if;
if new.empno>8000 THEN
SET new.empno = null;
end if;
end;

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 ;