How can I prevent locking issues between two triggers that fires at the same event on the same table?
The DB I’m working on has already one update trigger that is encrypted and therefore I cannot modify it. I made another update trigger to accomplish some new tasks, it’s working correctly when I test it directly on the database, but fails when I make an update to a product on the front-end application. Apparently, when I have my trigger active both triggers fails. The message I get is something like "Document is already open, I’ll increment it’s value".
Is this a locking issue?
There’s a related question where someone says we can have more than one trigger (for same event) on a table.
Here’s my triggers code:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[tr_st_rep_update]
ON [dbo].[st]
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;
IF ( update(ref)
OR update(design)
OR update(u_update)
OR update(u_ativo)
OR update(stock)
OR update(epv1)
OR update(epv2)
OR update(epv3)
OR update(peso)
OR update(u_catnv1)
OR update(u_catnv2)
OR update(u_catnv3)
OR update(u_dpromoi)
OR update(u_dpromof)
OR update(u_destaque) )
BEGIN
IF (SELECT count(*)
FROM Inserted
INNER JOIN Deleted
ON Inserted.ststamp = Deleted.ststamp
WHERE inserted.u_ativo = 1
OR ( Deleted.u_ativo = 1
AND Inserted.u_ativo = 0 )) > 0
BEGIN
INSERT INTO RepData
(id,
REF,
familia,
stock,
epv1,
epv2,
epv3,
peso,
u_accao,
imagem,
process)
SELECT Inserted.ststamp AS id,
Inserted.REF AS REF,
Inserted.familia AS familia,
Inserted.stock AS stock,
Inserted.epv1 AS epv1,
Inserted.epv2 AS epv2,
Inserted.epv3 AS epv3,
Inserted.peso AS peso,
CASE
WHEN Deleted.u_ativo = 1
AND Inserted.u_ativo = 0 THEN 'd'
ELSE 'u'
END AS u_accao,
Inserted.imagem AS imagem,
0 AS process
FROM Inserted
INNER JOIN Deleted
ON Deleted.ststamp = Inserted.ststamp
WHERE inserted.u_ativo = 1
OR ( Deleted.u_ativo = 1
AND Inserted.u_ativo = 0 )
END
END
END
Any help would be appreciated.
Update: Database is MSSQL 2008
Problem solved.
I really don’t know the source of the problem although I think it’s something related to table locking, in this case the on the Inserted table.
I just changed the inner select statement so that I grab the values directly from the st table instead of the Inserted.
Thanks everyone.