1、用数据库sa用户登录。
2、创建查询死锁的存储过程。--找引发锁的sqlcreate procedure pro_k坡纠课柩illsidasbegind髫潋啜缅eclare @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 sysprocesses where blocked>0 ) bwhere a.blocked=spid)union select spid,blocked from sysprocesses where blocked>0IF @@ERROR<>0 RETURN @@ERRORselect @intCountProperties = Count(*),@intCounter = 1from #tmp_lock_whoIF @@ERROR<>0 RETURN @@ERRORif @intCountProperties=0select '现在没有阻塞和死锁信息' as messagewhile @intCounter <= @intCountPropertiesbeginselect @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 )endset @intCounter = @intCounter + 1enddrop table #tmp_lock_whoreturn 0end
3、执行创建的存储过程。用master执行存储过程:execute pro_killsid
4、Kill掉死锁的进程SID号。
5、再用master执行存储过程:execute pro_killsid检测是否还有死锁。