【转】处理sql server 的死锁 --第一篇 --检测死锁 --如果发生死锁了,我们怎么去检测具体发生死锁的是哪条SQL 语句或存储过程
--这时我们可以使用以下存储过程来检测,就可以查出引起死锁的进程和SQL 语句
SQL Server自带的系统存储过程sp_who和sp_lock也可以用来查找阻塞和死锁, 但没有这里介绍的方法好用
use master go create procedure sp_who_lock as begin declare @spid int,@bl int, @intTransactionCountOnEntry int, @intRowcount int, @intCountProperties int, @intCounter int create table #tmp_lock_who ( id int identity(1,1), spid smallint, bl smallint) IF @@ERROR0 RETURN @@ERROR insert into #tmp_lock_who(spid,bl) select 0 ,blocked from (select * from sysprocesses where blocked>0 ) a where not exists(select * from (select * from sysprocesses where blocked>0 ) b where a
blocked=spid) union select spid,blocked from sysprocesses where blocked>0 IF @@ERROR0 RETURN @@ERROR -- 找到临时表的记录数 select @intCountProperties = Count(*),@int