一个数据库解锁的存储过程sp_who_lock
2008-04-10 13:33
465 查看
将所有死锁的死锁的id找出来,然后再kill。
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
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 @@ERROR<>0 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 @@ERROR<>0 RETURN @@ERROR
-- 找到临时表的记录数
select @intCountProperties = Count(*),@intCounter = 1
from #tmp_lock_who
IF @@ERROR<>0 RETURN @@ERROR
if@intCountProperties=0
select '现在没有阻塞和死锁信息' as message
-- 循环开始
while @intCounter <= @intCountProperties
begin
-- 取第一条记录
select @spid = spid,@bl = bl
from #tmp_lock_who where id = @intCounter
begin
if @spid =0
select '引起数据库死锁的是: '+ CAST(@bl AS VARCHAR(10)) + '进程号,其执行的SQL语法如下'
else
select '进程号SPID:'+ CAST(@spid AS VARCHAR(10))+ '被' + '进程号SPID:'+ CAST(@bl AS VARCHAR(10)) +'阻塞,其当前进程执行的SQL语法如下'
DBCC INPUTBUFFER (@bl )
end
-- 循环指针下移
set @intCounter = @intCounter + 1
end
drop table #tmp_lock_who
return 0
end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
死锁的
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
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 @@ERROR<>0 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 @@ERROR<>0 RETURN @@ERROR
-- 找到临时表的记录数
select @intCountProperties = Count(*),@intCounter = 1
from #tmp_lock_who
IF @@ERROR<>0 RETURN @@ERROR
if@intCountProperties=0
select '现在没有阻塞和死锁信息' as message
-- 循环开始
while @intCounter <= @intCountProperties
begin
-- 取第一条记录
select @spid = spid,@bl = bl
from #tmp_lock_who where id = @intCounter
begin
if @spid =0
select '引起数据库死锁的是: '+ CAST(@bl AS VARCHAR(10)) + '进程号,其执行的SQL语法如下'
else
select '进程号SPID:'+ CAST(@spid AS VARCHAR(10))+ '被' + '进程号SPID:'+ CAST(@bl AS VARCHAR(10)) +'阻塞,其当前进程执行的SQL语法如下'
DBCC INPUTBUFFER (@bl )
end
-- 循环指针下移
set @intCounter = @intCounter + 1
end
drop table #tmp_lock_who
return 0
end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
死锁的
相关文章推荐
- 一个数据库解锁的存储过程sp_who_lock
- 查看数据库死锁的存储过程SP_WHO_LOCK
- SQL2005查看死锁存储过程sp_who_lock
- 查看死锁 的存储过程 ,工具存储过程 sp_who_lock
- SQL : 如何完全复制一个服务器上的数据库A到另一个服务器上的数据库B(包括库A的所有表、试图、存储过程等等)
- 一个循环插入数据到数据库的存储过程
- sql2005 查看数据库或表大小的系统存储过程 sp_spaceused
- SQL作业--让数据库定期执行一个存储过程
- 一个截断多个数据库日志的的存储过程
- 一个查看MSSQLServer数据库空间使用情况的存储过程 SpaceUsed
- 数据库存储过程中定义一个变量,通过sql语句在数据表中获取这个变量值的方法。
- 分享一个收缩数据库日志文件的存储过程
- 一个值得研究的系统存储过程 sp_MSforeachtable
- 将一个数据库中的存储过程,复制到另一数据库
- ASE15.x之前版本中查看设备剩余空间以及设备上数据库分配信息的存储过程sp_helpdevice2
- 一个将数据导出到EXCEL的存储过程-数据库专栏,SQL Server
- 一个查询数据库所有表的存储过程(允许按表名模糊查询一级按显示数量进行分页)
- SQL SERVER 2000 连接ORCLE数据库 (加了一个存储过程的实例)
- 配置SharePoint 2007 过程中引发Microsoft.SharePoint.Upgrade.SPUpgradeException 异常(未能创建配置数据库)的一个解决方法
- 可以自定义导出数据库的表结构(视图)的一个存储过程