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;
Related
I am trying to write stored procedure to insets values and if record exist then select that row but it is giving me invalid use of group statement
I have written below: -
TRY 1:- error :- invalid use of group statement
CREATE DEFINER=`root`#`localhost` PROCEDURE `insusers`(IN enterprise_id varchar(40),IN enterprise_name varchar(40),IN email_id varchar(40))
BEGIN
SELECT COUNT(*) FROM user WHERE email_id = email_id;
IF COUNT(*) < 1 THEN
INSERT INTO user(enterprise_id,user_name,email_id)
VALUES (enterprise_id,enterprise_name,email_id);
SELECT * FROM user WHERE user_id = last_insert_id();
ELSE
SELECT * FROM user WHERE email_id = email_id;
END IF;
END
TRY 2 :- error :- Not able to insert new values
CREATE DEFINER=`root`#`localhost` PROCEDURE `insusers`(IN enterprise_id varchar(40),IN enterprise_name varchar(40),IN email_id varchar(40))
BEGIN
DECLARE count INT DEFAULT 0;
SELECT COUNT(*) INTO count FROM user WHERE email_id = email_id;
IF count < 1 THEN
INSERT INTO user(enterprise_id,user_name,email_id)
VALUES (enterprise_id,enterprise_name,email_id);
SELECT * FROM user WHERE user_id = last_insert_id();
ELSE
SELECT * FROM user WHERE email_id = email_id;
END IF;
END
Please help me with solution.
Thanks in advance.
It is recommended to use BEGIN and END to separate IF clauses, also use # to distinguish between variables and columns. Hope this helps:
CREATE DEFINER=`root`#`localhost` PROCEDURE `insusers`(IN #enterprise_id varchar(40),IN #enterprise_name varchar(40),IN #email_id varchar(40))
BEGIN
IF ((SELECT COUNT(*) FROM user WHERE email_id = #email_id)<1) BEGIN
INSERT INTO user(enterprise_id,user_name,email_id)
VALUES (#enterprise_id,#enterprise_name,#email_id);
SELECT * FROM user WHERE user_id = last_insert_id();
END ELSE BEGIN
SELECT * FROM user WHERE email_id = #email_id;
END
END
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 am setting triggerafter_inser and i dont know how to resolve it
DELIMITER $$
USE `solocloud_dev_2`$$
DROP TRIGGER IF EXISTS table_10_summary_after_insert $$
CREATE TRIGGER table_10_summary_after_insert AFTER INSERT ON table_10
FOR EACH ROW BEGIN
DECLARE var INT;
SET var=0;
SELECT COUNT(*) FROM table_10_summary WHERE client_id = new.client_id;
IF var > 0
THEN
UPDATE table_10_summary SET records = records+1 WHERE client_id = new.client_id;
ELSE
INSERT INTO table_10_summary (new.client_id,1);
END$$
DELIMITER$$
There are several problems with your trigger :
you need to SET var from the results of the query ; as it is now, you are not setting this value
the IF block must be terminated with END IF
The following codes compiles successfully in this DB Fiddle :
DELIMITER $$
DROP TRIGGER IF EXISTS table_10_summary_after_insert $$
CREATE TRIGGER table_10_summary_after_insert AFTER INSERT ON table_10
FOR EACH ROW
BEGIN
DECLARE var INT;
SET var = (SELECT COUNT(*) FROM table_10_summary WHERE client_id = new.client_id);
IF (var > 0) THEN
UPDATE table_10_summary SET records = records + 1 WHERE client_id = new.client_id;
ELSE
INSERT INTO table_10_summary VALUES(new.client_id,1);
END IF;
END$$
DELIMITER ;
I have 3 tables named opproduct, opcategory and opproduct_to_category.The opproduct table has an after insert and an after delete trigger. These triggers modify the category table content column, but it gets the category_id trough opproduct_to_category table. The statments of triggers roughly the same. The after update is working well, but after insert doesn't modify the category table.
Thanks for your answers!
DROP TRIGGER IF EXISTS opproduct_AINS;
DELIMITER $$
CREATE TRIGGER `opproduct_AINS` AFTER INSERT ON `opproduct`
FOR EACH ROW BEGIN
DECLARE _category_id INT;
IF new.`status`='1' THEN
SELECT category_id INTO _category_id
FROM opproduct_to_category
WHERE product_id = NEW.product_id;
UPDATE opcategory
SET content = content+1
WHERE category_id=_category_id;
END IF;
END$$
DELIMITER ;
DROP TRIGGER IF EXISTS opproduct_AUPD;
DELIMITER $$
CREATE TRIGGER `opproduct_AUPD` AFTER UPDATE ON `opproduct`
FOR EACH ROW BEGIN
DECLARE _category_id INT;
IF NEW.`status`<> OLD.`status` AND NEW.`status`=1 THEN
SELECT category_id INTO _category_id
FROM opproduct_to_category
WHERE product_id = NEW.product_id;
UPDATE opcategory SET content = content+1
WHERE category_id=_category_id;
ELSEIF NEW.`status`<> OLD.`status` AND NEW.`status`=0 THEN
SELECT category_id INTO _category_id
FROM opproduct_to_category
WHERE product_id = NEW.product_id;
UPDATE opcategory SET content = content-1
WHERE category_id=_category_id;
END IF;
END$$
DELIMITER ;
I wrote a trigger which get trigered after update statement of table1. If there is no row in table1 based on the conditions, then update table2.
CREATE TRIGGER trigger1
AFTER UPDATE ON my_database.table1
FOR EACH ROW
BEGIN
DECLARE cnt int;
IF new.column1 = 1 THEN
SELECT COUNT(1) INTO #cnt FROM my_database.table1
WHERE userID = 1 AND isDeleted = 0;
IF cnt = 0 THEN
UPDATE my_database.table2
SET isDeleted = 1
WHERE userSeqID = new.userID;
END IF
It gives me error i nline number 4, I couldn't understand the problem
Every IF statement has to ended with END IF clause. Try this statement -
DELIMITER $$
CREATE TRIGGER trigger1
AFTER UPDATE
ON my_database.table1
FOR EACH ROW
BEGIN
DECLARE cnt int;
IF NEW.column1 = 1 THEN
SELECT COUNT(1) INTO cnt FROM my_database.table1 WHERE userID = 1 AND isDeleted = 0;
IF cnt = 0 THEN
UPDATE my_database.table2 SET isDeleted = 1 WHERE userSeqID = NEW.userID;
END IF;
END IF;
END$$
DELIMITER ;
The error on line 4 is because you're not using a delimiter.
Try this:
DELIMITER //
create trigger trigger1 after update on my_database.table1
for each row
begin
declare cnt int;
...
end if;
END;//
DELIMITER ;
You'll also need an END IF on the line after the select statement, and a terminal END; to match your begin, as I have in my example.