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 ;
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've created this trigger, but I have two problems. One it does not actively populate the Person table and two. If I enter more than one row into my Party table I get an error in access, Result of Subquery returns more than one row.
Anybody know my problem(s)?
DELIMITER $$
Create TRIGGER Trigger1
AFTER
INSERT
ON Party
FOR EACH ROW
BEGIN
declare partytypeid int;
declare partyid int;
set #partyid:= (select partyid from party);
set #partytypeid:= (select partytypeid from party);
IF partytypeid = 1
THEN INSERT INTO Person
(PartyId) VALUES (PartyId);
END IF;
END$$
delimiter ;
When you set your variables, you're doing a select against the whole table instead of the inserted record.
DELIMITER $$
Create TRIGGER Trigger1
AFTER INSERT ON Party FOR EACH ROW
BEGIN
declare partytypeid int;
declare partyid int;
set #partyid:= new.partyid;
set #partytypeid:= new.partytypeid;
IF partytypeid = 1 THEN
INSERT INTO Person
(PartyId) VALUES (PartyId);
END IF;
END$$
delimiter ;
I have a problem with my trigger before insert.
I try to control the row number of my table 'user_search' for one 'id_user' and if this row number > 4, the oldest row should be deleted.
My actual trigger is this :
DELIMITER $$
CREATE TRIGGER before_insert_user_search
BEFORE INSERT
ON user_search FOR EACH ROW
BEGIN
DECLARE sDate DATETIME;
SELECT MIN(date_search) FROM user_search WHERE id_user = NEW.id_user INTO sDate;
IF ((SELECT COUNT(id) FROM user_search WHERE id_user = NEW.id_user) > 4) THEN
BEGIN
DELETE FROM user_search WHERE date_search = sDate;
END;
END IF;
END$$
DELIMITER ;
However, it does not work. Do someone have a solution to help me please.
Thanks.
You can not delete same table row during using INSERT trigger because it holds the lock of table. So try to write a Stored Procedure to insert new record.
Stored Procedure should be something like bellow:
CREATE PROCEDURE insert_user_search(new_id_user INT, ......other variable to inert)
BEGIN
DECLARE #minId INT;
//INSERT using parameter
SET #minId = SELECT MIN(id) FROM user_search WHERE id_user = new_id_user;
IF ((SELECT COUNT(id) FROM user_search WHERE id_user = new_id_user) > 4) THEN
BEGIN
DELETE FROM user_search WHERE id_user = #minId;
END;
END IF;
END;
I have created the following trigger in mysql:
DELIMITER $$
CREATE TRIGGER `some_trigger` AFTER INSERT ON db1.table
FOR EACH ROW
BEGIN
IF NEW.userid = 'certain_id' THEN
INSERT INTO db2.table
SET
value1 = NEW.value,
value2 = NEW.value;
END IF;
END $$
DELIMITER ;
The above statement works if I remove the if statement. Additionally no syntax errors are encountered when this trigger is added to the db. Any idea what is wrong with the if statement that is not allowing it to insert entries with value 'certain_id' in column userid when added to db1.table??
Try this it should work..
DELIMITER $$
CREATE TRIGGER `some_trigger` AFTER INSERT ON `db1`.`table`
FOR EACH ROW
BEGIN
IF (NEW.userid = 'certain_id') THEN
INSERT INTO `db2`.`table` (value1, value2)
VALUES (NEW.value1,NEW.value2);
END IF;
END $$
DELIMITER ;
This is my first time using trigger and I'm having a bit of trouble.
I'm creating a notification system that when a new notification is created it will add a row to the notify table. What is added depends on what notification type it is.
So far I've tried:
DELIMITER $$
DROP TRIGGER IF EXISTS generate_notify $$
CREATE TRIGGER generate_notify
AFTER INSERT
ON notifications
FOR EACH ROW
BEGIN
SELECT #group := notify_group FROM notification_types WHERE type=NEW.type;
IF (#group <> 1) THEN
INSERT INTO notify (user_id, notification_id) VALUES (NEW.user_reference, NEW.id);
ELSE
INSERT INTO notify (user_id, notification_id) VALUES(
SELECT ID, NEW.id FROM user_customer WHERE clientID=NEW.user_reference
);
END IF;
END; $$
DELIMITER ;
I searched around and then changed it to:
DELIMITER $$
DROP TRIGGER IF EXISTS generate_notify $$
CREATE TRIGGER generate_notify
AFTER INSERT
ON notifications
FOR EACH ROW
BEGIN
DECLARE insert_user_id INT(11);
SELECT #group := notify_group FROM notification_types WHERE type=NEW.type;
IF (#group <> 1) THEN
INSERT INTO notify (user_id, notification_id) VALUES (NEW.user_reference, NEW.id);
ELSE
SELECT ID INTO insert_user_id FROM user_customer WHERE clientID=NEW.user_reference;
INSERT INTO notify (user_id, notification_id) VALUES(insert_user_id, NEW.id);
END IF;
END; $$
DELIMITER ;
I've also tried mixing the above 2 up and all I'm getting is either can not return result set or there is a syntax error with my select query.
(Also, I'm assuming that the NEW keyword is predefined for triggers?)
Your first try was actually on the right track, but you did not write the result of your SELECT into a variable. Returning a result set from a trigger is not possible.
DELIMITER $$
DROP TRIGGER IF EXISTS generate_notify $$
CREATE TRIGGER generate_notify
AFTER INSERT
ON notifications
FOR EACH ROW
BEGIN
DECLARE v_group INT;
SELECT notify_group INTO v_group FROM notification_types WHERE type=NEW.type;
IF (v_group <> 1) THEN
INSERT INTO notify (user_id, notification_id) VALUES (NEW.user_reference, NEW.id);
ELSE
INSERT INTO notify (user_id, notification_id) VALUES(
SELECT ID, NEW.id FROM user_customer WHERE clientID=NEW.user_reference
);
END IF;
END $$
DELIMITER ;