Deadlock caused by SQL Server triggers

Source: Internet
Author: User
A trigger is triggered only when it is added. The purpose is to limit the ip address to send a text message for three minutes. The Access frequency of this table is very high for a certain period of time, the deadlock occurred during the peak hours today may be caused by the failure to judge the user's cause. Now let's take a look at the user's judgment. The code is roughly as follows: ALTERtrigger [dbo]. [trg_drv_y

A trigger is triggered only when it is added. The purpose is to limit the ip address to send a text message for three minutes. The Access frequency of this table is very high for a certain period of time, the deadlock occurred during the peak hours today may be caused by the failure to judge the user's cause. Now let's take a look at the user's judgment. The code is roughly as follows: ALTER trigger [dbo]. [trg_drv_y

A trigger is triggered only when it is added. The purpose is to limit the ip address to send a text message for three minutes. The Access frequency of this table is very high for a certain period of time, and a deadlock occurs during the peak hours today.

This may be because the user has not been determined. Add the user's judgment and try again. The code is roughly as follows:

ALTER trigger [dbo]. [trg_drv_yzm_insert] on [dbo]. [drv_yzm] for insert
As
Begin
If (CURRENT_USER = 'testuser ')
Begin
Declare @ lastcreatet varchar (50)
Declare @ lastdate datetime
Declare @ id int, @ clientip varchar (15), @ type varchar (20)

Select @ id = id, @ clientip = clientip, @ type = type from inserted;
Select top 1 @ lastcreatet = createt from drv_yzm where clientip = @ clientip and type = 'pre' and id <> @ id order by createt desc

Set @ lastdate = convert (datetime, substring (left (@ lastcreatet, 8) + ''+ substring (@ lastcreatet, 9,2) + ':' + substring (@ lastcreatet, 11,2) + ':' + substring (@ lastcreatet ))
Set @ lastdate = dateadd (mi, 3, @ lastdate)

If (@ lastdate> GETDATE () and @ type = 'pre ')
Update drv_yzm set status = '0' where id = @ id
End
End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.