I have a trigger that should work, but it does not work.
BEGIN
DECLARE `refresh` VARCHAR(256);
SET `refresh` = concat((SELECT `refresh` FROM `mobile_app_drivers` WHERE `id` = 1), '&');
UPDATE `mobile_app_drivers` SET `refresh` = #refresh WHERE `id` = 1;
END
When it is executed, the cell **refresh** still broadcasts an empty string.
// HOW I CREATE IT
DROP TRIGGER IF EXISTS `SET_UNIQUE_KEY`;
CREATE DEFINER=`root`#`localhost` TRIGGER `SET_UNIQUE_KEY` AFTER UPDATE ON `mobile_app_orders` FOR EACH ROW
BEGIN
DECLARE `refresh` VARCHAR(256);
SET `refresh` = concat((SELECT MAX(`refresh`) FROM `mobile_app_drivers` WHERE `id` = 1), '&');
UPDATE `mobile_app_drivers` SET `refresh` = #refresh WHERE `id` = NEW.driver;
END
I need, when any record from the table is updated, then a new substring was added from the value refresh from the table mobile_app_drivers.
Thank you!
You are using session variable #refresh. However, you have not defined it anywhere. So it will considered empty only.
I modified your trigger to make it more clear and logical to follow. Declared a new variable new_refresh and we can set its value using Into clause inside a Select statement.
Now, call the update statement.
Also, you need to redefine Delimiter to something else (eg: $$), instead of (;).
At the end, redefine the DELIMITER to ;
You can modify your Create Trigger statement to as follows:
DELIMITER $$
DROP TRIGGER IF EXISTS `SET_UNIQUE_KEY` $$
CREATE DEFINER=`root`#`localhost` TRIGGER `SET_UNIQUE_KEY` AFTER UPDATE ON `mobile_app_orders` FOR EACH ROW
BEGIN
DECLARE new_refresh VARCHAR(256) DEFAULT '';
SELECT CONCAT(MAX(`refresh`), '&') INTO `new_refresh`
FROM `mobile_app_drivers`
WHERE `id` = 1;
UPDATE `mobile_app_drivers` SET `refresh` = new_refresh WHERE `id` = NEW.driver;
END $$
DELIMITER ;
Why are you using a variable? Just do:
DROP TRIGGER IF EXISTS `SET_UNIQUE_KEY`;
DELIMITER $$
CREATE DEFINER=`root`#`localhost` TRIGGER `SET_UNIQUE_KEY` AFTER UPDATE ON `mobile_app_orders`
FOR EACH ROW
BEGIN
UPDATE mobile_app_drivers CROSS JOIN
(SELECT MAX(refresh) as max_refresh
FROM mobile_app_drivers
WHERE id = 1
) mad1
SET refresh = concat(mad1.max_refresh, '&')
WHERE id = NEW.driver;
END;
DELIMITER ;
Related
Is it possible to insert a control on the data that insert in a table? In a way that satisfies a specific condition provided, otherwise not
drop function Presente;
delimiter //
create function Presente( Persona_Da_Cercare varchar(16), Data_Legge date) returns int
begin
declare risultato int;
select count(*) into risultato
from PARLAMENTARI_IN_CARICA as pic
where Persona_Da_Cercare = pic.Persona and data_Legge >= pic.Data_Eletto;
if risultato = 0
then
select count(*) into risultato
from STORICO_PARLAMENTARI as sp
where Persona_Da_Cercare = sp.Persona and data_Legge >= sp.Data_Inizio and data_Legge <= sp.Data_Fine;
end if;
return risultato;
end//
set #ultima_legge bigint;
set #ultimo_parlamentare varchar(16);
set #data_legge date;
drop trigger if exists Ultimo_Voto;
delimiter $$
create trigger Ultimo_Voto before insert on VOTO_PALESE
for each row
begin
set #ultima_legge = new.Legge;
set #ultimo_parlamentare = new.parlamentare;
set #data_legge = (select Data_Approvazione from LEGGI where Cod_Legge = new.Legge);
end $$
drop trigger if exists Controllo_Voto_Palese;
delimiter $$
create trigger Controllo_Voto_Palese after insert on VOTO_PALESE
for each row
BEGIN
IF Presente(#ultimo_parlamentare, #data_legge) = 0 THEN
delete from VOTO_PALESE where Legge = #ultima_legge AND Parlamentare = #ultimo_parlamentare;
END IF;
END $$
insert into VOTO_PALESE(Legge, Parlamentare, Voto) values(770, 'MSRMRC01C14L378X', 'astenuto');
when I run the code it gives me this error
Error Code: 1442. Can't update table 'voto_palese' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
I don't understand where the problem is
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.
I am trying to create a mysql trigger. The error code 1064 is very generic. The trigger is to see if a record exists for a particular key; and if it does update a particular column in the newly inserted row with that value. I am using a select query on the same table as the trigger is on. Does that cause a problem? Here is the code:
DELIMITER $$
CREATE TRIGGER classid_insert
AFTER INSERT ON courses_semester
FOR EACH ROW
BEGIN
SET #class_id = (SELECT class_id FROM courses_semester WHERE
course_id = NEW.`course_id`
AND `dept_id` = NEW.`dept_id`
AND `univ_id` = NEW.`univ_id`
AND `section_id` = NEW.`section_id`
AND `semester` = NEW.`semester`
AND `year` = NEW.`year`);
IF(#class_id = NULL)
THEN
NEW.class_id = UUID()
ELSE
NEW.class_id = #class_id
END IF;
END
DELIMITER ;
your entire trigger code should look like below. remove the spaces after DELIMITER $$ and before DELIMITER ;. Also, the last END need to changed to END $$. Also, it has to be an BEFORE trigger and not an AFTER trigger.
Moreover, NEW.class_id = UUID() is not correct.It's missing SET as
SET NEW.class_id = UUID();
CORRECTED TRIGGER CODE:
DELIMITER $$
CREATE TRIGGER classid_insert
BEFORE INSERT ON courses_semester
FOR EACH ROW
BEGIN
SET #class_id = (SELECT class_id FROM courses_semester WHERE
course_id = NEW.`course_id`
AND `dept_id` = NEW.`dept_id`
AND `univ_id` = NEW.`univ_id`
AND `section_id` = NEW.`section_id`
AND `semester` = NEW.`semester`
AND `year` = NEW.`year`);
IF(#class_id = NULL)
THEN
SET NEW.class_id = UUID();
ELSE
SET NEW.class_id = #class_id;
END IF;
END $$
DELIMITER ;
CREATE FUNCTION TWO_FRONT(txt CHAR(30))
RETURNS CHAR(2)
RETURN SUBSTRING(txt, 1, 2);
DELIMITER $$
CREATE FUNCTION CHECK_AVABILITY(t_nama CHAR(30))
RETURNS INT(4)
BEGIN
DECLARE vreturn INT(4);
DECLARE P CHAR(8);
SET p = (SELECT MAX(id) FROM tabel_user WHERE nama like CONCAT(TWO_FRONT(t_nama),'%'));
IF ISNULL(p)THEN
SET vreturn = 0;
ELSE
SET vreturn = SUBSTRING(p, 4, 4);
END IF;
RETURN vreturn;
END $$
DELIMITER;
CREATE FUNCTION COMBINE(fn CHAR(2), nu CHAR(4))
RETURNS CHAR(7)
RETURN CONCAT(UPPER(fn),'-',nu);
CREATE TRIGGER cast_id AFTER INSERT ON tabel_user
FOR EACH ROW
BEGIN
UPDATE tabel_user SET id = COMBINE(TWO_FRONT(OLD.nama),CHECK_AVABILITY(OLD.nama)) WHERE id = OLD.id;
END;
Everyting is okay but after i insert this...
INSERT INTO tabel_user VALUES('','Blabla');
error is coming...
ERROR 1442 (HY000): Can't update table 'tabel_user' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Can you help me...
You're sending MySQL mixed messages.
cast_id fires after INSERT, but it is looking for OLD.nama and OLD.id. OLD, on the other hand, only exists when there is an UPDATE being called. I think you meant:
CREATE TRIGGER cast_id AFTER INSERT ON tabel_user
FOR EACH ROW
BEGIN
-- notice no "UPDATE" or "WHERE", "SET" implies "set on this current row"
SET NEW.id = COMBINE(TWO_FRONT(NEW.nama),CHECK_AVABILITY(NEW.nama));
END;
Currently, I have bunch of triggers that do the same thing. This is copy-pasted for every table that needs this functionality.
delimiter $$
create trigger user_before_insert before insert on user for each row
begin
set NEW.create_time = CURRENT_TIMESTAMP;
set NEW.create_user_id = #user_id;
set NEW.modify_time = CURRENT_TIMESTAMP;
set NEW.modify_user_id = #user_id;
end$$
create trigger user_before_update before update on user for each row
begin
set NEW.modify_time = CURRENT_TIMESTAMP;
set NEW.modify_user_id = #user_id;
end$$
Is it possible to wrap the lines that modify OLD and/or NEW into stored procedures that are called via triggers? Something like this:
delimiter $$
create procedure autofill_on_insert(inout NEW data_type) -- what would be the data_type?
begin
set NEW.create_time = CURRENT_TIMESTAMP;
set NEW.create_user_id = #user_id;
set NEW.modify_time = CURRENT_TIMESTAMP;
set NEW.modify_user_id = #user_id;
end$$
create procedure autofill_on_update(inout NEW data_type) -- what would be the data_type?
begin
set NEW.modify_time = CURRENT_TIMESTAMP;
set NEW.modify_user_id = #user_id;
end$$
delimiter ;
create trigger user_before_insert before insert on user
for each row call autofill_on_insert(NEW);
create trigger user_before_update before update on user
for each row call autofill_on_update(NEW);
Additional question: if this is possible, is there any way to check if NEW contains specific columns? There are tables that do not have modify_time and modify_user_id.
I can say that NEW cannot be passed into the procedure, because NEW is an alias that represents a row. Procedure's arguments have to be scalar values, like INT, VARCHAR, etc..
About the 'SET NEW.create_time = CURRENT_TIMESTAMP;'; if field create_time is a TIMESTAMP, you could set CURRENT_TIMESTAMP as default value for it.