Call procedure inside trigger in mysql - mysql

I want to update counter table when user table is modofied.
I have created a trigger where i will cal this procedure.
But this procudeure is not creating.
Please tell me what error in this procedure.
CREATE PROCEDURE barproc(IN id int,IN val int)
BEGIN
DECLARE #total_products int;
set #total_products=(SELECT COUNT(*)
FROM user where a=id and status='active')
if(#total_products>0)
update counter
set b=val
WHERE a = id and status='active';
END if;

Try
DELIMITER $$
CREATE PROCEDURE barproc(IN id INT, IN val INT)
BEGIN
IF (SELECT COUNT(*)
FROM `user`
WHERE a = id AND `status` = 'active') THEN
UPDATE counter
SET b = val
WHERE a = id AND `status` = 'active';
END IF;
END$$
DELIMITER ;

Related

Trigger befor insert

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

how to fix error in mysql code while making count trigger after insert

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 ;

Trigger Before Insert MySql

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;

Create a trigger after insert with a select in mysql

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 ;

MySql stored functions: Not works

I have the following function:
DROP FUNCTION IF EXISTS saveTableRow;
DELIMITER $$
CREATE FUNCTION saveTableRow(adapter_id int(10), view_id int(10),name varchar(255)) RETURNS TINYINT(1)
BEGIN
DECLARE retOK TINYINT DEFAULT 0;
IF (SELECT COUNT(*) FROM `tables` WHERE `adapter_id`=adapter_id AND `view_id`=view_id AND `name`=name ) = 0 THEN
INSERT INTO `tables` (`adapter_id`,`view_id`,`name`) VALUES (adapter_id, view_id, name);
SET retOK = 1;
END IF;
RETURN retOK;
END;
$$
DELIMITER ;
When i call the function to insert a new row with
SELECT saveTableRow(3,1,'Text');
I get the result '0' and there is no new row saved.
It might be a name collision problem. The name of the column is the same with the name of you parameter. You need to change the name of your parameter that is different from the name of your column. eg,
DROP FUNCTION IF EXISTS saveTableRow;
DELIMITER $$
CREATE FUNCTION saveTableRow(
_adapter_id int(10),
_view_id int(10),
_name varchar(255))
RETURNS TINYINT(1)
BEGIN
DECLARE retOK TINYINT DEFAULT 0;
IF (SELECT COUNT(*)
FROM `tables`
WHERE `adapter_id`=_adapter_id AND
`view_id`=_view_id AND
`name`=_name ) = 0 THEN
INSERT INTO `tables` (`adapter_id`,`view_id`,`name`)
VALUES (_adapter_id, _view_id, _name);
SET retOK = 1;
END IF;
RETURN retOK;
END;
$$
DELIMITER ;
change the if as follows as try please:
IF ((SELECT COUNT(*) FROM `tables` WHERE `adapter_id`=adapter_id AND `view_id`=view_id AND `name`=name ) < 1)