Re: Trigger Deadlock
- From: Erland Sommarskog <esquel@xxxxxxxxxxxxx>
- Date: Mon, 25 Jun 2007 21:56:22 +0000 (UTC)
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]
SET NOCOUNT ON
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.
PRINT 'Unexpected Error Occurred!'
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
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
Books Online for SQL Server 2000 at
- Trigger Deadlock
- From: DennBen
- Trigger Deadlock
- Prev by Date: Re: MSSQL - DTS Package - Find distinct rows - Output to TXT file - ActiveX?
- Next by Date: Re: SQL 2000 date format problem after migration W2k to W2k3
- Previous by thread: Trigger Deadlock
- Next by thread: compare 2 values in same solumn