MySQL Forums
Forum List  »  Triggers

LAST_INSERT_ID () in trigger
Posted by: douglas fabiano specht
Date: September 15, 2015 08:58AM

Hello,
I need to create an audit trigger when every insert I reply that table. It happens that this table has a auto_increment field, in this case how can I do to get the value of the next value of the auto_increment, tried to use the the function N = (select LAST_INSERT_ID ()), adding 1 more, but he always writes one, never key value of the table ..

follows my trigger


DELIMITER //
CREATE TRIGGER diretopg BEFORE INSERT ON direto FOR EACH ROW

BEGIN
DECLARE N int(11);
declare dtagendamento datetime;
set N=(select LAST_INSERT_ID())+1;
if new.agendamento = '0000-00-00 00:00:00' then set dtagendamento = null;
end if;


INSERT INTO diretopg values(
N,
new.remetente ,
new.destinatario,
new.textosms,
new.campanha ,
new.dataenvio ,
dtagendamento,
new.status ,
new.situacao,
new.tipo ,
new.ag ,
new.qnt ,
new.sms ,
new.prioridade,
new.cpf);
END; //

DELIMITER ;

Options: ReplyQuote


Subject
Views
Written By
Posted
LAST_INSERT_ID () in trigger
4886
September 15, 2015 08:58AM
1450
September 15, 2015 12:04PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.