Update the same row after insert Inside the trigger - mysql

I have to use a trigger that will update the inserted row. Following is my trigger.
DELIMITER $$
USE `kikan_db`$$
CREATE TRIGGER `t_trigger` AFTER INSERT ON `t_order`
FOR EACH ROW BEGIN
Declare kb_rate int;
Declare kb_amount int;
REPLACE INTO t_order_sales_month
SET order_no = NEW.order_no,
search_sales_month = getSalesMonth(NEW.order_no);
SELECT kickback_rate INTO kb_rate from m_customer where customer_code = New.bill_customer_code;
SET kb_amount := 9999 / kb_rate;
UPDATE `t_order` SET `t_order`.kickback_amount = kb_amount where `t_order`.order_no = NEW.order_no;
END;
$$
DELIMITER ;
Getting error at update.
It is mandatory for me to use AFTER instead of BEFORE type of trigger

Instead of using only after insert/update trigger, (1) you can change the value of virtual new row on before insert/update :
Declare kb_rate int;
Declare kb_amount int;
SELECT kickback_rate INTO kb_rate from m_customer
where customer_code = New.bill_customer_code;
SET kb_amount := 9999 / kb_rate;
SET new.kickback_amount = kb_amount;
(2)And execute the other SQL statements in a after insert/update trigger:
REPLACE INTO t_order_sales_month
SET order_no = NEW.order_no,
search_sales_month = getSalesMonth(NEW.order_no);

Related

Create mysql trigger before insert (Syntax error)

Hi i am creating some trigger for update the value in insert query before insert the row like this
CREATE TRIGGER `Insert members in Posts` BEFORE INSERT ON
`posts` FOR EACH ROW
BEGIN
DECLARE name_ varchar(100);
DECLARE contact_ varchar(16);
DECLARE status_ int;
DECLARE deleted_ int;
SELECT
`members`.`name`,
`members`.`contact`,
`members`.`status`,
`members`.`deleted`
INTO name_, contact_, status_, deleted_
FROM
`members`
WHERE
`members`.`member_id` = NEW.member_id;
SET NEW.member_name = name_;
SET NEW.member_contact = contact_;
SET NEW.member_status = status_;
SET NEW.member_deleted = deleted;
END
But i received the error like
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4
I am little confused what's the error in line no 4. Please explain someone. Thanks.
Depending on the client/ide used you may need to set delimiter for example
drop trigger if exists t;
delimiter $$
CREATE TRIGGER t BEFORE INSERT ON
`posts` FOR EACH ROW
BEGIN
DECLARE name_ varchar(100);
DECLARE contact_ varchar(16);
DECLARE status_ int;
DECLARE deleted_ int;
SELECT
`members`.`name`,
`members`.`contact`,
`members`.`status`,
`members`.`deleted`
INTO name_, contact_, status_, deleted_
FROM
`members`
WHERE
`members`.`member_id` = NEW.member_id;
SET NEW.member_name = name_;
SET NEW.member_contact = contact_;
SET NEW.member_status = status_;
SET NEW.member_deleted = deleted;
END $$
delimiter ;
please review https://dev.mysql.com/doc/refman/8.0/en/stored-routines.html

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 ;

Error in if condition on mysql trigger

I have written a small trigger function in MySQL . This is the trigger query i have written and the condition are not checked direct default value of created_id is assigned.
CREATE TRIGGER `complain_attend_log` AFTER INSERT ON `attend_complain`
FOR EACH ROW BEGIN
DECLARE attend_created INT;
DECLARE created_id INT default 5;
SET attend_created = (SELECT attend_created FROM allotments WHERE complain_id = NEW.complain_id);
IF(attend_created = 1) THEN
SET created_id = 6;
END IF;
INSERT INTO complain_logs
VALUES(null,NEW.complain_id,NEW.allotment_id,NEW.attend_complain_id,NEW.is_closed,created_id,1,now(),1,now());
END

not allowed to return a resultset from a trigger mysql

delimiter $$
CREATE TRIGGER REDUCE_NOTE_COUNT
AFTER DELETE ON iv_notes
FOR EACH ROW BEGIN
DECLARE supplierid int(11);
DECLARE customerid int(11);
SELECT supplierid ,customerid FROM iv_documents WHERE id=OLD.note_documentid;
SET supplierid=supplierid;
SET customerid=customerid;
IF supplierid=OLD.note_companyid THEN
update iv_documents
set supplier_notes=supplier_notes-1
where id=OLD.note_documentid and supplier_notes>0;
END IF;
IF customerid=OLD.note_companyid THEN
update iv_documents set customer_notes=customer_notes-1
where id=OLD.note_documentid
and customer_notes>0 ;
END IF;
END$$
delimiter ;
You cannot execute SELECT statements from trigger. If you want to set variables, then use SELECT INTO statement, e.g. -
DECLARE supplierid_ INT(11);
DECLARE customerid_ INT(11);
SELECT
supplierid, customerid
INTO
supplierid_, customerid_
FROM
iv_documents
WHERE
id = OLD.note_documentid;
IF supplierid_ = OLD.note_companyid THEN
...
Also, rename variables, they have to differ from from field names.