Solved

SQL Tricky Trigger causing deadlocks

Posted on 2011-09-23
3
390 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

632 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