Solved

SQL Tricky Trigger causing deadlocks

Posted on 2011-09-23
3
383 Views
Last Modified: 2012-05-12
This Trigger works with just 1 device moving through.  It starts to lose it the more you throw at it.

IF EXISTS (SELECT 1 FROM sysobjects WHERE name = 'AuthCodesCompare1' AND type = 'TR')
DROP TRIGGER  AuthCodesCompare1
GO
CREATE TRIGGER AuthCodesCompare1 ON [AuthCodesCompare]
FOR INSERT
AS

DECLARE @AuthCodesCompareID As INT
DECLARE @DeviceID As INT
DECLARE @RecNum As varchar(50)
DECLARE @OperatorCode As varchar(50)
DECLARE @OpName As varchar(50)
DECLARE @OpType As varchar(50)
DECLARE @OpExpire As varchar(50)

DECLARE @OpNameSup As varchar(50)
DECLARE @OpTypeSup As varchar(50)
DECLARE @OpExpireSup As varchar(50)

SELECT @AuthCodesCompareID = (SELECT AuthCodesCompareID FROM Inserted)
SELECT @DeviceID = (SELECT DeviceID FROM Inserted)
SELECT @RecNum = (SELECT RecNum FROM Inserted)
SELECT @OperatorCode = (SELECT OperatorCode FROM Inserted)
SELECT @OpName = (SELECT OpName FROM Inserted)
SELECT @OpType = (SELECT OpType FROM Inserted)
SELECT @OpExpire = (SELECT OpExpire FROM Inserted)

      If exists
      (      
      select * from [AuthCodes]
      Where      [DeviceID] = @DeviceID AND
                  [OperatorCode] = @OperatorCode AND
                  [OpName] = @OpName AND
                  [OpType] = @OpType AND
                  [OpExpire] = @OpExpire                  
                  
      )
BEGIN
    UPDATE      [AuthCodesCompare]
    SET [State] = 'ADD'
      WHERE [AuthCodesCompareID] = @AuthCodesCompareID  AND [RecNum] = ''
     
END

     --------------------
      If exists
      (      
      select * from [AuthCodesCompare]
      Where      [DeviceID] = @DeviceID AND
                  [OperatorCode] = @OperatorCode AND
                  [RecNum]<>''
      )
BEGIN
    UPDATE      [AuthCodesCompare]
    SET [State] = 'SUPERSEDED'
      WHERE [STATE] LIKE '%ADD%' AND [OperatorCode] = @OperatorCode
END

     --------------------
      If exists
      (      
      select * from [AuthCodesCompare]
      Where      [DeviceID] = @DeviceID AND
                  [OperatorCode] = @OperatorCode AND
                  [OpName] = @OpName AND
                  [OpType] = @OpType AND
                  [OpExpire] = @OpExpire AND
                  [RecNum]=''
      )
BEGIN
    UPDATE      [AuthCodesCompare]
    SET [State] = 'MATCH'
      WHERE [RecNum]<>'' AND [OperatorCode] = @OperatorCode
END

     --------------------
      If exists
      (      
      select * from [AuthCodesCompare]
      Where      [DeviceID] = @DeviceID AND
                  [OperatorCode] = @OperatorCode AND
                  (
                  [OpName] <> @OpName OR
                  [OpType] <> @OpType OR
                  [OpExpire] <> @OpExpire
                  ) AND
                  [RecNum]=''
      )
BEGIN
    SELECT @OpNameSup = (SELECT OpName FROM [AuthCodesCompare] Where [OperatorCode] = @OperatorCode AND [RecNum]='')
      SELECT @OpTypeSup = (SELECT OpType FROM [AuthCodesCompare] Where [OperatorCode] = @OperatorCode AND [RecNum]='')
      SELECT @OpExpireSup = (SELECT OpExpire FROM [AuthCodesCompare] Where [OperatorCode] = @OperatorCode AND [RecNum]='')
   
    UPDATE      [AuthCodesCompare]
    SET [State] = 'EDIT',
    [OpName] = @OpNameSup,
    [OpType] = @OpTypeSup,
    [OpExpire] = @OpExpireSup
      WHERE [RecNum]<>'' AND [OperatorCode] = @OperatorCode
END

     --------------------
      If not exists
      (      
      select * from [AuthCodesCompare]
      Where      [DeviceID] = @DeviceID AND
                  [OperatorCode] = @OperatorCode AND
                  [RecNum]=''
      )
BEGIN
    UPDATE      [AuthCodesCompare]
    SET [State] = 'DELETE'
      WHERE [RecNum]<>'' AND [OperatorCode] = @OperatorCode
END
GO
0
Comment
Question by:MBoy
3 Comments
 
LVL 3

Author Comment

by:MBoy
ID: 36586907
Any help would be appreciated
0
 
LVL 2

Expert Comment

by:marappan
ID: 36588044
Does this trigger involved in another process for deadlock to occur? (or) Is a different call to the same trigger involves in deadlock?
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 36590971
This is a more rational way to use TRIGGERs and it supports INSERTs with multiple rows.  To be honest some of your code does not make sense, but I have translated it exactly.
CREATE TRIGGER AuthCodesCompare1 ON AuthCodesCompare
    FOR INSERT

AS

IF EXISTS ( SELECT  1
            FROM    AuthCodes a
                    INNER JOIN INSERTED i ON a.DeviceID = i.DeviceID
                                             AND a.OperatorCode = i.OperatorCode
                                             AND a.OpName = i.OpName
                                             AND a.OpType = i.OpType
                                             AND a.OpExpire = i.OpExpire ) 
    BEGIN
        UPDATE  a
        SET     [State] = 'ADD'
        FROM    AuthCodesCompare a
                INNER JOIN INSERTED i ON a.AuthCodesCompareID = i.AuthCodesCompareID
        WHERE   a.RecNum = ''
     
    END

     --------------------
IF EXISTS ( SELECT  1
            FROM    AuthCodesCompare a
                    INNER JOIN INSERTED i ON a.DeviceID = i.DeviceID
                                             AND a.OperatorCode = i.OperatorCode
            WHERE   a.RecNum <> '' ) 
    BEGIN
        UPDATE  a
        SET     [State] = 'SUPERSEDED'
        FROM    AuthCodesCompare a
                INNER JOIN INSERTED i ON a.OperatorCode = i.OperatorCode
        WHERE   a.[STATE] LIKE '%ADD%'

    END

     --------------------
IF EXISTS ( SELECT  1
            FROM    AuthCodesCompare a
                    INNER JOIN INSERTED i ON a.DeviceID = i.DeviceID
                                             AND a.OperatorCode = i.OperatorCode
                                             AND a.OpName = i.OpName
                                             AND a.OpType = i.OpType
                                             AND a.OpExpire = i.OpExpire
            WHERE   a.RecNum = '' ) 
    BEGIN
        UPDATE  a
        SET     [State] = 'MATCH'
        FROM    AuthCodesCompare a
                INNER JOIN INSERTED i ON a.OperatorCode = i.OperatorCode
        WHERE   a.RecNum <> ''

    END

     --------------------
IF EXISTS ( SELECT  1
            FROM    AuthCodesCompare a
                    INNER JOIN INSERTED i ON a.DeviceID = i.DeviceID
                                             AND a.OperatorCode = i.OperatorCode
                                             AND (a.OpName <> i.OpName
                                                  OR a.OpType <> i.OpType
                                                  OR a.OpExpire <> i.OpExpire
                                                 )
            WHERE   a.RecNum = '' ) 
    BEGIN
        UPDATE  a
        SET     [State] = 'EDIT',
                OpName = a.OpName,
                OpType = a.OpType,
                OpExpire = a.OpExpire
        FROM    AuthCodesCompare a
                INNER JOIN INSERTED i ON a.OperatorCode = i.OperatorCode
        WHERE   a.RecNum <> ''
    END

     --------------------
IF NOT EXISTS ( SELECT  1
                FROM    AuthCodesCompare a
                        INNER JOIN INSERTED i ON a.DeviceID = i.DeviceID
                                                 AND a.OperatorCode = i.OperatorCode
                WHERE   a.RecNum = '' ) 
    BEGIN
        UPDATE  a
        SET     [State] = 'DELETE'
        FROM    AuthCodesCompare a
                INNER JOIN INSERTED i ON a.OperatorCode = i.OperatorCode
        WHERE   a.RecNum <> ''
    END
GO

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now