The process of looking for a locked table

Source: Internet
Author: User

The following procedure can find the deadlock process

use [ksoa]goset ansi_nulls ongoset quoted_identifier ongocreate procedure [ DBO]. [Sp_who_lock] asbegindeclare  @spid  int, @bl  int, @intTransactionCountOnEntryint, @intRowcountint, @ Intcountpropertiesint, @intCounterintcreate  table  #tmp_lock_who   ( id int identity (,  spid smallint,  bl smallint ) if @ @ERROR <>0 return  @ @ERRORinsert  into  #tmp_lock_who (SPID,BL)  select 0, blockedfrom  (select  * from master.dbo.sysprocesses where  blocked>0 )  awhere not  EXISTS  (select * from  (select * from master.dbo.sysprocesses  where  blocked>0)  b where spid=a.blocked) union select spid,blocked  from master.dbo.sysprocesses where  blocked>0if @ @ERROR <>0 RETURN  @ @ERROR-- Number of records found for temporary table select@intcountproperties = count (*),  @intCounter  = 1from  #tmp_lock_ whoif @ @ERROR <>0 return @ @ERRORif @intcountproperties = 0select  ' No blocking and deadlock information now! '  as MESSAGE--  cycle begins while  @intCounter  <=  @intCountPropertiesBEGIN--  Take the first record select@spid = spid,  @bl  = blfrom  #tmp_lock_who  where id =   @intCounterbeginif   @spid  = 0select  ' cause database deadlock is:   process number ' + cast (@bl   As varchar  +  ',  The SQL syntax it executes is as follows: ' elseselect  ' process number spid: ' + cast (@spid  as  varchar () +  ' is blocked by the process number spid: ' + cast (@bl  as varchar ()  + ') and its current process executes the following SQL syntax: ' DBCC  INPUTBUFFER  (@bl  ) end--  loop pointer down set  @intCounter  =  @intCounter  +  1enddrop table  #tmp_lock_whoreturn  0end


The process of looking for a locked table

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.