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;
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
Here is my Trigger
DELIMITER $$
CREATE TRIGGER before_insert BEFORE INSERT ON SampleTable
FOR EACH ROW
BEGIN
IF NEW.flag =0 THEN
SET NEW.media = "../trigger/smile1.png";
ELSEIF NEW.flag = 1 THEN
SET NEW.media = "../trigger/smile2.png";
ELSEIF NEW.flag = 2 THEN
SET NEW.media = "../trigger/smile3.png";
END IF;
CALL Media (NEW.flag);
END$$
DELIMITER ;
The trigger was created successfully. And I called stored procedure from a trigger.
procedure
DELIMITER $$
CREATE PROCEDURE Media (a INT)
BEGIN
SELECT firstname, lastname, media FROM SampleTable WHERE flag = a;
END $$
DELIMITER ;
When I use insert query it shows the following error.
not allowed to return a result set from a trigger
I found select query is a reason for the error. But I don't know how to solve it.
Any Help...
I need to update media cell based on flag value after the insert trigger occurred.
My code is ...
DELIMITER $$
CREATE TRIGGER after_insert AFTER INSERT ON SampleTable
FOR EACH ROW
BEGIN
UPDATE SampleTable SET media = '../trigger/smile1.png' WHERE flag =0;
UPDATE SampleTable SET media = '../trigger/smile2.png' WHERE flag =1;
UPDATE SampleTable SET media = '../trigger/smile3.png' WHERE flag =2;
END$$
DELIMITER ;
Trigger created successfully...
But when I try to insert values, it shows the following error.
"Can't update table 'SampleTable' in stored function/trigger because it is already used by statement which invoked this stored function/trigger."
I don't understand what is the exact problem.
any help...
DELIMITER $$
CREATE TRIGGER before_user_insert BEFORE INSERT ON SampleTable
FOR EACH ROW
BEGIN
IF NEW.flag =0 THEN
SET NEW.media = "../trigger/smile1.png";
ELSEIF NEW.flag = 1 THEN
SET NEW.media = "../trigger/smile2.png";
ELSEIF NEW.flag = 2 THEN
SET NEW.media = "../trigger/smile3.png";
END IF;
END$$
DELIMITER ;
I have two table 'testing1' with fields 'firstname' and 'lastname'. Another table 'testing2' with field 'firstname'.
So the trigger checks first whether the 'NEW.firstname' exists in 'testing2' table or not. If it does then it doesn't execute the INSERT query but if it doesn't exist then the INSERT query is executed and 'NEW.firstname' is added in 'testing2' table.
Here's the trigger that i created ... but I'm getting error in the IF loop ...
DELIMITER $$;
CREATE TRIGGER testRef AFTER INSERT ON testing1
FOR EACH ROW
BEGIN
DECLARE rowCount INTEGER;
SET #rowCount := ( SELECT COUNT(firstname) FROM testing2 WHERE testing2.firstname = NEW.firstname );
IF (rowCount)
INSERT INTO testing2 (firstname) VALUES (NEW.firstname);
END $$
I'm unable to figure out where did I made the mistake... Any help ??
Try
DELIMITER $$;
CREATE TRIGGER testRef AFTER INSERT ON testing1
FOR EACH ROW
BEGIN
DECLARE rowCount INTEGER;
SET #rowCount := ( SELECT COUNT(firstname) FROM testing2 WHERE testing2.firstname = NEW.firstname );
IF (rowCount) THEN
INSERT INTO testing2 (firstname) VALUES (NEW.firstname);
END IF;
END$$
DELIMITER ;
But maybe you shoud use IF (rowCount)>0
This worked for me :
DELIMITER //
CREATE TRIGGER testRef AFTER INSERT ON testing1
FOR EACH ROW
BEGIN
DECLARE rowCount INTEGER;
SELECT COUNT(firstname) FROM testing2 WHERE testing2.firstname = NEW.firstname INTO rowCount;
IF rowCount = 0 THEN
INSERT INTO testing2 (firstname) VALUES (NEW.firstname);
END IF;
END //
DELIMITER ;
Can someone please tell me whats wrong with this trigger statement?
DELIMITER //
CREATE TRIGGER something AFTER INSERT ON sometable
FOR EACH ROW
BEGIN
DECLARE var INT DEFAULT 0;
SET var = SELECT COUNT(*) FROM anothertable;
IF var=0 THEN
INSERT INTO anothertable values(`x`,`y`,`z`);
END IF;
END//
I keep getting error saying i have syntax errors...
Try putting the SELECT statement within brackets:
DELIMITER //
CREATE TRIGGER something AFTER INSERT ON sometable
FOR EACH ROW
BEGIN
DECLARE var INT DEFAULT 0;
SET var = (SELECT COUNT(*) FROM anothertable);
IF var=0 THEN
INSERT INTO anothertable values('x','y','z');
END IF;
END//