Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Tricky Trigger causing deadlocks

Posted on 2011-09-23
3
Medium Priority
?
391 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 2000 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

705 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