I work with SQL Server 2008 R2
I have a simple trigger
CREATE TRIGGER [dbo].[T_Personne_ITrig] ON [dbo].[Personne] FOR INSERT AS
BEGIN
SET NOCOUNT ON
insert into syn_HistoriquePersonne
(hpers_Timestamp, Supprime, ID, Nom, Prenom, Champ1,
Champ2, Champ3, Champ4 SiteAssocie)
select GETDATE(), 0, ID, Nom, Prenom, Champ1, Champ2, Champ3,
Champ4, SiteAssocie
from inserted
END
It works properly. Problem is, I work on a program with an horrible code base so my boss don't want to trigger to ever cause a rollback on the table Personne even if it fails. I know it's really improbable, but he's scared of timeout in case of huge database activity... ANYWAY
So I searched about committing in triggers. And changed the trigger to :
CREATE TRIGGER [dbo].[T_Personne_ITrig] ON [dbo].[Personne] FOR INSERT AS
BEGIN
SET NOCOUNT ON
COMMIT
insert into syn_HistoriquePersonne
(hpers_Timestamp, Supprime, ID, Nom, Prenom, Champ1,
Champ2, Champ3, Champ4 SiteAssocie)
select GETDATE(), 0, ID, Nom, Prenom, Champ1, Champ2, Champ3,
Champ4, SiteAssocie
from inserted
END
But the trigger kept shooting message
Transaction stopped in trigger, batch aborted.
So I made it like that :
CREATE TRIGGER [dbo].[T_Personne_ITrig] ON [dbo].[Personne] FOR INSERT AS
BEGIN
SET NOCOUNT ON
COMMIT
BEGIN TRAN
insert into syn_HistoriquePersonne
(hpers_Timestamp, Supprime, ID, Nom, Prenom, Champ1,
Champ2, Champ3, Champ4, SiteAssocie)
select GETDATE(), 0, ID, Nom, Prenom, Champ1, Champ2, Champ3,
Champ4, SiteAssocie
from inserted
END
It stopped doing the batch aborted, but it seems to never insert anything in my historic table... I read about the subject and this should work I think. But it doesn't...
Anyone else already had that problem and how can I fix that?
I am doing simple insert into to test my trigger.
ROLLBACKin your trigger - then you should be fine. Don't start new transactions in a trigger... the trigger should always be executed in the context of the operation that caused it to fire.