/*********************************** //删除 死锁 存储过程***************************************/if exists (select * from dbo.sysobjectswhere id = object_id(N'[dbo].[sp_who_lock]')and OBJECTPROPERTY(id, N'IsProcedure') = 1)drop procedure [dbo].[sp_who_lock]GO/********************************************************// 日期 :2015-03-20// 说明 : 查看数据库里阻塞和死锁情况********************************************************/use mastergocreate procedure sp_who_lockasbegindeclare @spid int,@bl int,@intTransactionCountOnEntry int,@intRowcount int,@intCountProperties int,@intCounter intcreate table #tmp_lock_who (id int identity(1,1),spid smallint,bl smallint)IF @@ERROR<>0 RETURN @@ERRORinsert into #tmp_lock_who(spid,bl) select 0 ,blockedfrom (select * from sysprocesses where blocked>0 ) awhere not exists(select * from (select * from sysprocesseswhere blocked>0 ) bwhere a.blocked=spid)union select spid,blocked from sysprocesses where blocked>0IF @@ERROR<>0 RETURN @@ERROR-- 找到临时表的记录数select @intCountProperties = Count(*),@intCounter = 1from #tmp_lock_whoIF @@ERROR<>0 RETURN @@ERRORif @intCountProperties=0select '现在没有阻塞和死锁信息' as message-- 循环开始while @intCounter <= @intCountPropertiesbegin-- 取第一条记录select @spid = spid,@bl = blfrom #tmp_lock_who where Id = @intCounterbeginif @spid =0select '引起数据库死锁的是: '+ CAST(@bl AS VARCHAR(10))+ '进程号,其执行的SQL语法如下'elseselect '进程号SPID:'+ CAST(@spid AS VARCHAR(10))+ '被'+ '进程号SPID:'+ CAST(@bl AS VARCHAR(10)) +'阻塞,其当前进程执行的SQL语法如下'DBCC INPUTBUFFER (@bl )end-- 循环指针下移set @intCounter = @intCounter + 1enddrop table #tmp_lock_whoreturn 0end