Re: Trigger Deadlock



DennBen (dbenedett@xxxxxxxxxxx) writes:
I am doing an update to set a field value = anothe field value (in the
same table) where it is not supplied. I'm handling this in the
trigger, but am getting deadlocks.


Do you see anything wrong with this that would cause deadlocking?


ALTER TRIGGER [trg_myTable_UPDATE]
ON [dbo].[myTable]
AFTER UPDATE,INSERT
AS

SET NOCOUNT ON
BEGIN TRANSACTION
UPDATE A
SET A.MarketID = A.SiteID
FROM myTable A
INNER JOIN INSERTED B
ON A.UID = B.UID
WHERE B.MarketID IS NULL;

IF (@@ERROR <> 0)
BEGIN -- if...then for error handling
RAISERROR 20000 'trg_myTable_UPDATE Update Trigger Failed.
Transaction aborted.'
PRINT 'Unexpected Error Occurred!'
ROLLBACK TRANSACTION
END
ELSE
COMMIT TRANSACTION

First, don't include BEGIN or COMMIT TRANSACTION in a trigger. A trigger
always runs in the context of the transaction defined by the statement
that fired it, so there is no need for user-defined transaction. But
by adding them, and doing things a bit wrong can cause unexpected
consequences.

ROLLBACK TRANSACTION in case you detect a violation of a business rule,
is of course OK.

Checking for @@error is a bit over-the-mark, since an error in a trigger
aborts the entire batch on the spot.

As for why you are getting deadlocks, there is too little information
to tell. Do you know what the trigger is deadlocking with?

There are two trace flags you can enable in the startup options for
SQL Server. When these are active, a deadlock trace is written to the
SQL Server error log. While cryptic, this information can be helpful
to understand why the deadlock is happening. On SQL 2000, you need to
add -T1204 and -T3605 to the startup options. On SQL 2005, use -T1222 and
-T3605. (1222 gives more information than 1204.)

--
Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
.



Relevant Pages

  • Re: SQL 7 vs. 2000 issue -trigger and nulls
    ... >We're having trouble with a trigger updating some tables. ... course be locked by the current transaction, ... locking data and updating rows when the COMMIT inside the trigger is ... I seriously hope that SQL Server 7.0 simply disregarded these two ...
    (microsoft.public.sqlserver.mseq)
  • Re: counting rows
    ... The trigger is a simple look up via a unique index, ... isolation is that no other connections can insert rows within the key ... the row that the other transaction is insering is not visible to the ... to demonstrate what i'm talking about - current version is SQL Server ...
    (comp.databases.theory)
  • Re: Transaction Abort in a trigger SQL 2008, writing to event log
    ... In SQL Server though, it is not handled cleanly, and you ... So if the error occured in the trigger, capture it, raise it up, ... And if you pature an error in a catch block, and do not re-raise it, it ... I always get the message that the transaction was ...
    (microsoft.public.sqlserver.programming)
  • Re: Need Advice !
    ... Triggers are just one aspect of using SQL Server Service Broker. ... trigger fails, then nothing the trigger was doing to put data into a ... Once the first half of the transaction has been successfully committed ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Transaction Deadlocks your thoughts?
    ... The rules for minimizing deadlocks are ... you to "soft lock" any resource you need prior to going into transaction ... Wayne Snyder, MCDBA, SQL Server MVP ...
    (microsoft.public.sqlserver.programming)