那就建一个 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 enddrop table #tmp_lock_whoreturn 0 end GO
:sp_who 或 sp_who2 2: Select * from sysprocesses where blocked <> 0 3: 企业管理器->服务器->管理工具->活动->当前活动 然后把他kill掉 4:SQL事件探查器,监控一下,看主要是那些处理引起的死锁.然后做相应的处理. 用事件探查器new一个trace,监视一下造成你sqlserver停顿的情况
最好的办法还是检查一下引起锁的原因,一般是由你的代码引起的。
/* 功 能:查看sqlserver的表 锁定情况(用户、进程、表) 作 者:金振水 日 期:2009-12-15 备 注:在sqlserver2000上调试通过; 所查找的对象 到数据库的基本 objects 级别 欢迎修改完善!*/declare @dbname sysname set @dbname = 'usercenter' declare @objname sysname set @objname = 'tp_uidinfo' declare @exec varchar(500)--准备进程快照 create table #proc(spid int,dbid int,objid bigint,indid bigint, [type] varchar(10),resource varchar(30), mode varchar(10),status varchar(10)) insert into #proc exec sp_lock create table #t1(dbid int,dbname sysname ,objid bigint,obj_name sysname,obj_type varchar(20)) set @exec = 'select db_id(''' + @dbname + ''') as dbid,''' + @dbname + '''as dbname,id,name,type from ' + @dbname + '.dbo.sysobjects where name like ''%' + @objname + '%''' insert into #t1 execute(@exec) --准备用户信息快照 create table #who(spid int,ecid int,status varchar(20),loginame varchar(256),hostname varchar(256), blk int,dbname varchar(50),cmd varchar(32)) insert into #who exec sp_who --查询锁资源信息(注意引起死锁的进程spid) select a.spid,c.loginame,c.hostname,c.blk as deaded_spid,c.cmd,c.status as user_status,a.dbid,b.dbname,a.objid,b.obj_name,b.obj_type,a.indid,a.type,a.resource,a.mode,a.status from #proc a inner join #t1 b on a.dbid = b.dbid and a.objid = b.objid inner join #who c on a.spid=c.spid where type in ('TAB') --可以注释掉以获取更详细的资料 order by a.spid,blk--清理临时表 drop table #t1 drop table #proc drop table #who--sp_who
exec sp_lock
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
enddrop table #tmp_lock_whoreturn 0
end GO
2: Select * from sysprocesses where blocked <> 0
3: 企业管理器->服务器->管理工具->活动->当前活动 然后把他kill掉
4:SQL事件探查器,监控一下,看主要是那些处理引起的死锁.然后做相应的处理.
用事件探查器new一个trace,监视一下造成你sqlserver停顿的情况
最好的办法还是检查一下引起锁的原因,一般是由你的代码引起的。
功 能:查看sqlserver的表 锁定情况(用户、进程、表)
作 者:金振水
日 期:2009-12-15
备 注:在sqlserver2000上调试通过;
所查找的对象 到数据库的基本 objects 级别
欢迎修改完善!*/declare @dbname sysname
set @dbname = 'usercenter'
declare @objname sysname
set @objname = 'tp_uidinfo'
declare @exec varchar(500)--准备进程快照
create table #proc(spid int,dbid int,objid bigint,indid bigint, [type] varchar(10),resource varchar(30), mode varchar(10),status varchar(10))
insert into #proc exec sp_lock create table #t1(dbid int,dbname sysname ,objid bigint,obj_name sysname,obj_type varchar(20))
set @exec = 'select db_id(''' + @dbname + ''') as dbid,''' + @dbname + '''as dbname,id,name,type from ' + @dbname + '.dbo.sysobjects where name like ''%' + @objname + '%'''
insert into #t1 execute(@exec)
--准备用户信息快照
create table #who(spid int,ecid int,status varchar(20),loginame varchar(256),hostname varchar(256), blk int,dbname varchar(50),cmd varchar(32))
insert into #who exec sp_who
--查询锁资源信息(注意引起死锁的进程spid)
select a.spid,c.loginame,c.hostname,c.blk as deaded_spid,c.cmd,c.status as user_status,a.dbid,b.dbname,a.objid,b.obj_name,b.obj_type,a.indid,a.type,a.resource,a.mode,a.status
from #proc a inner join #t1 b on a.dbid = b.dbid and a.objid = b.objid inner join #who c on a.spid=c.spid
where type in ('TAB') --可以注释掉以获取更详细的资料
order by a.spid,blk--清理临时表
drop table #t1
drop table #proc
drop table #who--sp_who
有sp_who
有sp_lock
楼主混在一块了吧?
即:
库名..sp_who_lock或是从能执行的库上,找到这个过程生成脚本,然后在你当前库执行一下
执行存储过程要加上exec
我是WINDOWS身份验证登录的。
查看sp_who_lock 存储过程