http://blog.sina.com.cn/s/blog_9dcdd2020101nf4v.html

sqlserver检测死锁;杀死锁和进程;查看锁信息 (2013-06-09 16:43:03)转载▼
标签: sql 检测死锁 杀死锁和进程 查看锁信息 it 分类: mssql --检测死锁 --如果发生死锁了,我们怎么去检测具体发生死锁的是哪条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 @@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 --杀死锁和进程 --如何去手动的杀死进程和锁?最简单的办法,重新启动服务。但是这里要介绍一个存储过程,通过显式的调用,可以杀死进程和锁。 use master go if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_killspid]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[p_killspid] GO create proc p_killspid @dbname varchar(200) --要关闭进程的数据库名
as declare @sql nvarchar(500) declare @spid nvarchar(20) declare #tb cursor for select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id(@dbname) open #tb fetch next from #tb into @spid while @@fetch_status=0 begin exec('kill '+@spid) fetch next from #tb into @spid end close #tb deallocate #tb go --用法
exec p_killspid 'newdbpy' --查看锁信息 --如何查看系统中所有锁的详细信息?在企业管理管理器中,我们可以看到一些进程和锁的信息,这里介绍另外一种方法。 --查看锁信息
create table #t(req_spid int,obj_name sysname) declare @s nvarchar(4000) ,@rid int,@dbname sysname,@id int,@objname sysname declare tb cursor for select distinct req_spid,dbname=db_name(rsc_dbid),rsc_objid from master..syslockinfo where rsc_type in(4,5) open tb fetch next from tb into @rid,@dbname,@id while @@fetch_status=0 begin set @s='select @objname=name from ['+@dbname+']..sysobjects where id=@id' exec sp_executesql @s,N'@objname sysname out,@id int',@objname out,@id insert into #t values(@rid,@objname) fetch next from tb into @rid,@dbname,@id end close tb deallocate tb select 进程id=a.req_spid ,数据库=db_name(rsc_dbid) ,类型=case rsc_type when 1 then 'NULL 资源(未使用)' when 2 then '数据库' when 3 then '文件' when 4 then '索引' when 5 then '表' when 6 then '页' when 7 then '键' when 8 then '扩展盘区' when 9 then 'RID(行 ID)' when 10 then '应用程序' end ,对象id=rsc_objid ,对象名=b.obj_name ,rsc_indid from master..syslockinfo a left join #t b on a.req_spid=b.req_spid go drop table #t

sqlserver检测死锁;杀死锁和进程;查看锁信息的更多相关文章

  1. Innodb中怎么查看锁信息

    一.前言 上一篇说了下innodb中锁的大概意思, 这篇说说怎么查看加的哪些锁.不然后续出现死锁或者锁等待都不知道为什么. 二.底层基础表信息 在学会如何查看有哪些锁信息时, 需要了解一些基础表信息, ...

  2. 查看锁信息(开启InnoDB监控)

    当前mysql版本:5.6.21 一.背景 在mysql处理死锁问题时,由于show engine innodb status输出来的死锁日志无任务事务上下文,并不能很好地诊断相关事务所持有的所有锁信 ...

  3. 查看锁信息 v$lock 和 v$locked_object

    查看锁住的对象及会话id,serial# select a.*  from (SELECT o.object_name,               l.locked_mode,            ...

  4. SQL Server 2008中查看锁信息

    ;with tran_locks as(select resource_type,db_name(resource_database_id) as db_name,resource_descripti ...

  5. SQL查看死锁+清理死锁

    ----查看sql死锁 CREATE procedure sp_who_lock    as      begin         declare @spid int         declare ...

  6. SQL SEVER 死锁// 解除死锁

    SQL SEVER 死锁 USE mastergo CREATE PROC killspid (@dbname VARCHAR (20))AS   BEGIN      DECLARE @sql NV ...

  7. sqlserver锁机制详解(sqlserver查看锁)

    简介 在SQL Server中,每一个查询都会找到最短路径实现自己的目标.如果数据库只接受一个连接一次只执行一个查询.那么查询当然是要多快好省的完成工作.但对于 大多数数据库来说是需要同时处理多个查询 ...

  8. db2 查杀死锁进程

    db2 查杀死锁进命令 db2 get snapshot for locks on (需要snapshot的访问权限) db2 list applications db2 "force ap ...

  9. mysql死锁-查询锁表进程-分析锁表原因【转】

    查询锁表进程: 1.查询是否锁表 show OPEN TABLES where In_use > 0;   2.查询进程     show processlist   查询到相对应的进程===然 ...

随机推荐

  1. js学习笔记8----定时器

    1.setInterval 和 setTimeout:定时器 语法:setInterval(函数名,时间间隔); 如:setInterval(fn(),1000);         //表示每隔1秒执 ...

  2. 新版汉诺塔(UVa10795 - A Different Task)

    题目介绍: 标准的汉诺塔上有n个大小各异的盘子.现给定一个初始局面(见图1),求它到目标局面(见图2)至少需要移动多少步? 移动规则:一次只能移动一个盘子:且在移动盘子之前,必须把压在上面的其他盘子先 ...

  3. BZOJ 1115: [POI2009]石子游戏Kam

    1115: [POI2009]石子游戏Kam Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 883  Solved: 545[Submit][Stat ...

  4. css-IE中的border-radius和box-shadow

    1.今天遇到一个问题,在给一个图片设置border-radius时,firefox和chrome表现正常,但是在IE中就无效,在IE中必须给图片设置一个边框,比如设置一个透明的边框就可以正常显示. 2 ...

  5. NOIP2013

    DAY1 转圈游戏 列出式子(x+km)%n,快速幂. // codevs3285 #include<algorithm> #include<iostream> #includ ...

  6. 【bzoj2038】 小Z的袜子(hose)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2038 (题目链接) 转自:http://blog.csdn.net/bossup/article/d ...

  7. squid安装配置

    Squid做反向代理(192.168.1.69) squid.conf http_port 80 vhost vport visible_hostname pdd2.matrixcdn.net cac ...

  8. StringBuilder类与String类的区别

    String对象是不可改变的,每次使用String类中的方法时,都要在内存中创建一个新的字符串对象,这就需要为该新对象分配新的空间.在需要对字符串执行重复修改的情况下,与创建新的String对象相关的 ...

  9. Math.Round四舍五入

    Math.Round函数四舍五入的问题   今天客户跑过来跟我说,我们程序里面计算的价格不对,我检查了一下,发现价格是经过折算后的价格,结果是可能小数位较多,而单据上只能打印两位价格,所以就对价格调用 ...

  10. string字符串的一系列操作

    IndexOf() 查找字串中指定字符或字串首次出现的位置,返首索引值,如: str1.IndexOf("字"): //查找“字”在str1中的索引值(位置) str1.Index ...