当前位置: 数据库>sqlserver
一个查询Sql server数据库死锁的存储过程
来源: 互联网 发布时间:2014-08-29
本文导语: 一般在sql server中,当死锁出现以后,维护人员或开发人员大多通过sp_who来查找死锁的进程,然后用sp_kill杀掉。 本文创建一个存储过程sp_who_lock,可以快速查询出是哪个进程出现了死锁,出现死锁的问题在什么地方? sp_who_lock...
一般在sql server中,当死锁出现以后,维护人员或开发人员大多通过sp_who来查找死锁的进程,然后用sp_kill杀掉。
本文创建一个存储过程sp_who_lock,可以快速查询出是哪个进程出现了死锁,出现死锁的问题在什么地方?
sp_who_lock存储过程创建脚本:
代码示例:
CREATE procedure sp_who_lock
as
begin
declare @spid int
declare @blk int
declare @count int
declare @index int
declare @lock tinyint
set @lock=0
create table #temp_who_lock
(
id int identity(1,1),
spid int,
blk int
)
if @@error0 return @@error
insert into #temp_who_lock(spid,blk)
select 0 ,blocked
from (select * from master..sysprocesses where blocked>0)a
where not exists(select * from master..sysprocesses where a.blocked =spid and blocked>0)
union select spid,blocked from master..sysprocesses where blocked>0
if @@error0 return @@error
select @count=count(*),@index=1 from #temp_who_lock
if @@error0 return @@error
if @count=0
begin
select '没有阻塞和死锁信息'
return 0
end
while @index@index and exists(select 1 from #temp_who_lock where id
as
begin
declare @spid int
declare @blk int
declare @count int
declare @index int
declare @lock tinyint
set @lock=0
create table #temp_who_lock
(
id int identity(1,1),
spid int,
blk int
)
if @@error0 return @@error
insert into #temp_who_lock(spid,blk)
select 0 ,blocked
from (select * from master..sysprocesses where blocked>0)a
where not exists(select * from master..sysprocesses where a.blocked =spid and blocked>0)
union select spid,blocked from master..sysprocesses where blocked>0
if @@error0 return @@error
select @count=count(*),@index=1 from #temp_who_lock
if @@error0 return @@error
if @count=0
begin
select '没有阻塞和死锁信息'
return 0
end
while @index@index and exists(select 1 from #temp_who_lock where id