error in trigger creation also how to optimise it - mysql

Please tell me whats wrong with the code. Not able to fine the bug
DELIMITER $$
CREATE TRIGGER update_status BEFORE Update ON listing_basic_new_updated
FOR EACH ROW
if new.processing_status is not null
then begin
SET new.rep_status = New.processing_status;
end;
elseif new.televeri_status is not null
then begin
SET new.rep_status = New.televeri_status;
end;
elseif new.verification_status is not null
then begin
SET new.rep_status = New.verification_status;
end;
end if;
END$$
DELIMITER ;

I think you're missing a BEGIN that would match up to your END at the end:
DELIMITER $$
CREATE TRIGGER update_status BEFORE Update ON listing_basic_new_updated
FOR EACH ROW
BEGIN
if new.processing_status is not null
then begin
SET new.rep_status = New.processing_status;
end;
elseif new.televeri_status is not null
then begin
SET new.rep_status = New.televeri_status;
end;
elseif new.verification_status is not null
then begin
SET new.rep_status = New.verification_status;
end;
end if;
END$$
DELIMITER ;
I think you might be able to replace all of it with
SET new.rep_status = COALESCE(new.processing_status, new.televeri_status,
new.verification_status, new.rep_status);
COALESCE: "Returns the first non-NULL value in the list, or NULL if there are no non-NULL values."

Related

Mysql Trigger Error with IF THEN

I have a problem with trigger.. I would like to change a value to false if new returnBike is not null.
CREATE TRIGGER returnBikeTrigger
AFTER UPDATE ON Rent
FOR EACH ROW
IF (NEW.returnBike <> null) THEN
UPDATE Bike SET isRented = false
WHERE id = (select Bike from Rent); -- but there is Error (missing semicolon)
END IF;
Any advice?
I would be inclined to just do:
DELIMITER $$
CREATE TRIGGER returnBikeTrigger
AFTER UPDATE ON Rent
FOR EACH ROW
BEGIN
UPDATE Bike
SET isRented = false
WHERE id = new.Bike AND NEW.returnBike IS NOT NULL;
END; $$
DELIMITER ;
DELIMITER $$
CREATE TRIGGER returnBikeTrigger
AFTER UPDATE ON Rent
FOR EACH ROW
BEGIN
IF (NEW.returnBike IS NOT NULL) THEN
UPDATE BikeSET isRented = false
WHERE id = new.Bike;
END IF;
END; $$
DELIMITER ;
so to remove problem with syntax semicolon added delimiter and $$ after end;
Anyway thanks to give me some advice with if, comparing!

Trigger to change empty values to NULL

I am trying to write a trigger where if an incoming value is empty (in other words ''), then insert NULL in the table. I have :
DELIMITER //
CREATE TRIGGER avoid_empty
BEFORE INSERT ON EVALUATION
FOR EACH ROW
BEGIN
IF mark = '' THEN SET NEW.mark = NULL;
END IF;
END;
//
DELIMITER ;
Which executes without errors, but it doesn't do what I need.
Try:
DELIMITER //
CREATE TRIGGER `avoid_empty` BEFORE INSERT ON `EVALUATION`
FOR EACH ROW
BEGIN
IF NEW.`mark` = '' THEN
SET NEW.`mark` := NULL;
END IF;
END//
DELIMITER ;
See db-fiddle.

MySQL Function getting Syntax Error

I am trying to create the below function in MySQL but getting syntax error.
I am not able to find the solution, would be grateful for some help
CREATE FUNCTION `test`.`pro`(depart_id int) RETURNS varchar
BEGIN
DECLARE title varchar;
if depart_id = 1 then
set title='IT Department';
else if depart_id = 2 then
set title='HR Department';
else
set title='Admin';
end if;
return title;
END$$
DELIMITER ;
You have several syntax errors in your script:
varchar must have a length
You should define DELIMITER $$ first
It's not else if, but elseif
Try this;)
DELIMITER $$
CREATE FUNCTION `test`.`pro`(depart_id int) RETURNS varchar(10)
BEGIN
DECLARE title varchar(10);
if depart_id = 1 then
set title='IT Department';
elseif depart_id = 2 then
set title='HR Department';
else
set title='Admin';
end if;
return title;
END $$
DELIMITER ;

Using a stored proceedure in multiple triggers

In addition to the notes table, I am applying this same trigger to around 15 other tables. Not sure, but seems like a stored procedure will eliminate the duplicated code. Is this a good application for a stored procedure? If so, how could it be implemented?
CREATE TRIGGER tg_notes_ins BEFORE INSERT ON notes FOR EACH ROW
BEGIN
IF NEW.created_by_user IS NULL OR NEW.created_by_user = '' THEN
SET NEW.created_by_user = #users_id;
END IF;
IF NEW.modified_by_user IS NULL OR NEW.modified_by_user = '' THEN
SET NEW.modified_by_user = #users_id;
END IF;
END$$
CREATE TRIGGER tg_notes_upd BEFORE UPDATE ON notes FOR EACH ROW
BEGIN
IF NEW.modified_by_user = OLD.modified_by_user THEN
SET NEW.modified_by_user = #users_id;
END IF;
END$$
I am not claiming this is how to do it, but it seems to work, and would appreciate any opinions. Thanks
DELIMITER $$
CREATE PROCEDURE `createRecord` ()
BEGIN
IF NEW.created_by_user IS NULL OR NEW.created_by_user = '' THEN
SET NEW.created_by_user = #users_id;
END IF;
IF NEW.modified_by_user IS NULL OR NEW.modified_by_user = '' THEN
SET NEW.modified_by_user = #users_id;
END IF;
END$$
CREATE PROCEDURE `modifyRecord` ()
BEGIN
IF NEW.modified_by_user = OLD.modified_by_user THEN
SET NEW.modified_by_user = #users_id;
END IF;
END$$
CREATE TRIGGER tg_notes_upd BEFORE UPDATE ON notes FOR EACH ROW
BEGIN
CALL createRecord();
END$$
CREATE TRIGGER tg_notes_ins BEFORE INSERT ON notes FOR EACH ROW
BEGIN
CALL modifyRecord();
END$$
DELIMITER ;

MySql Trigger won't compute

I need to create a trigger that computes one column value based on other column values. A database is a little bit denormalized to get higher performance. (Normalization is not an issue of this question).
The problem is that I want to set value of computed_address value and it is ok if i put a constant in it. But it seems that these If clauses aren't working and I just can't see the problem.
Below is the trigger code.
Thank you very much!
DELIMITER $$
USE `nth_poi_new_3`$$
DROP TRIGGER /*!50032 IF EXISTS */ `poi_address_creator`$$
CREATE
/*!50017 DEFINER = 'root'#'localhost' */
TRIGGER `poi_address_creator` BEFORE INSERT ON `poi`
FOR EACH ROW BEGIN
DECLARE full_address VARCHAR(255);
DECLARE country_string VARCHAR(100);
DECLARE region_string VARCHAR(100);
DECLARE town_string VARCHAR(100);
DECLARE address_string VARCHAR(100);
IF NEW.address <> '' THEN
SET full_address = CONCAT(NEW.address, ",");
END IF;
IF NEW.town_name IS NOT NULL THEN
SET full_address = CONCAT(full_address, NEW.town_name, ",");
ELSEIF NEW.town_id IS NOT NULL THEN
SELECT NAME INTO town_string FROM town WHERE town.town_id = NEW.town_id LIMIT 1;
SET full_address = CONCAT(full_address, town_string, ",");
END IF;
IF NEW.region_name IS NOT NULL THEN
SET full_address = CONCAT(full_address, NEW.region_name, ",");
ELSEIF NEW.region_id IS NOT NULL THEN
SELECT NAME INTO region_string FROM region WHERE region.region_id = NEW.region_id LIMIT 1;
SET full_address = CONCAT(full_address, region_string, ",");
END IF;
IF NEW.country_name IS NOT NULL THEN
SET full_address = CONCAT(full_address, NEW.country_name, ",");
ELSEIF NEW.country_id IS NOT NULL THEN
SELECT NAME INTO country_string FROM country WHERE country.country_id = NEW.country_id LIMIT 1;
SET full_address = CONCAT(full_address, country_string, ",");
END IF;
SET NEW.computed_address = full_address;
END;
$$
DELIMITER ;
at the bottom of your trigger code
END;
$$
DELIMITER ;
should be
END$$
DELIMITER ;
Hey,
just to answer. I had to initialize full_address variable first. I just made this:
DECLARE full_address VARCHAR(255);
But after declaration it needed to be initialization also:
SET full_address="";