/*

exec master.dbo.p_lockinfo 0,0  ---显示死锁的进程,不显示正常的进程

exec master.dbo.p_lockinfo 1,0  ---杀死死锁的进程,不显示正常的进程

*/

Create  proc p_lockinfo
@kill_lock_spid bit=1,  --是否杀掉死锁的进程,1 杀掉, 0 仅显示
@show_spid_if_nolock bit=1 --如果没有死锁的进程,是否显示正常进程信息,1 显示,0 不显示
as

declare @count int,@s nvarchar(1000),@i int
select id=identity(int,1,1),标志,
进程ID=spid,线程ID=kpid,块进程ID=blocked,数据库ID=dbid,
数据库名=db_name(dbid),用户ID=uid,用户名=loginame,累计CPU时间=cpu,
登陆时间=login_time,打开事务数=open_tran, 进程状态=status,
工作站名=hostname,应用程序名=program_name,工作站进程ID=hostprocess,
域名=nt_domain,网卡地址=net_address
into #t from(
select 标志='死锁的进程',
  spid,kpid,a.blocked,dbid,uid,loginame,cpu,login_time,open_tran,
  status,hostname,program_name,hostprocess,nt_domain,net_address,
  s1=a.spid,s2=0
from master..sysprocesses a join (
  select blocked from master..sysprocesses group by blocked
  )b on a.spid=b.blocked where a.blocked=0
union all
select '|_牺牲品_>',
  spid,kpid,blocked,dbid,uid,loginame,cpu,login_time,open_tran,
  status,hostname,program_name,hostprocess,nt_domain,net_address,
  s1=blocked,s2=1
from master..sysprocesses a where blocked<>0
)a order by s1,s2

select @count=@@rowcount,@i=1

if @count=0 and @show_spid_if_nolock=1
begin
insert #t
select 标志='正常的进程',
  spid,kpid,blocked,dbid,db_name(dbid),uid,loginame,cpu,login_time,
  open_tran,status,hostname,program_name,hostprocess,nt_domain,net_address
from master..sysprocesses
set @count=@@rowcount
end

if @count>0
begin
create table #t1(id int identity(1,1),a nvarchar(30),b Int,EventInfo nvarchar(255))
if @kill_lock_spid=1
begin
  declare @spid varchar(10),@标志 varchar(10)
  while @i<=@count
  begin
   select @spid=进程ID,@标志=标志 from #t where id=@i
   insert #t1 exec('dbcc inputbuffer('+@spid+')')
   if @标志='死锁的进程' exec('kill '+@spid)
   set @i=@i+1
  end
end
else
  while @i<=@count
  begin
   select @s='dbcc inputbuffer('+cast(进程ID as varchar)+')' from #t where id=@i
   insert #t1 exec(@s)
   set @i=@i+1
  end
select a.*,进程的SQL语句=b.EventInfo
from #t a join #t1 b on a.id=b.id
end

sql server死锁的查询和解除相关推荐

  1. sql server死锁_如何使用扩展事件和SQL Server代理自动执行SQL Server死锁收集过程

    sql server死锁 介绍 (Introduction) This article is the last one of a series in which we discussed how to ...

  2. sql server死锁_SQL Server死锁定义和概述

    sql server死锁 介绍 (Introduction) In this series, I will provide all of the information you need to und ...

  3. sql server死锁_如何解决SQL Server中的死锁

    sql server死锁 In this article, we will talk about the deadlocks in SQL Server, and then we will analy ...

  4. sql server死锁_如何报告SQL Server死锁事件

    sql server死锁 介绍 (Introduction) In the previous article entitled "What are SQL Server deadlocks ...

  5. sql活动监视器 死锁_监视SQL Server死锁–简单方法

    sql活动监视器 死锁 SQL Server is a very powerful tool and wherever I go, I see the tool being way much unde ...

  6. sql server死锁_了解SQL Server中的死锁图的XML描述

    sql server死锁 介绍 (Introduction) 在我的前两篇文章" What is a SQL Server Deadlock and 什么是SQL Server死锁" ...

  7. sql server死锁_了解SQL Server死锁图的图形表示

    sql server死锁 #contentor img { float: left; } #contentor img { float: left; } 介绍 (Introduction) If yo ...

  8. sql活动监视器 死锁_使用system_health扩展事件监视SQL Server死锁

    sql活动监视器 死锁 Performance monitoring is a must to do the task for a DBA. You should ensure that the da ...

  9. sql server死锁_了解SQL Server中的死锁定义

    sql server死锁 This article explains the deadlock definition in SQL Server, and it also mentions how t ...

最新文章

  1. 转[WinForm] VS2010发布、打包安装程序(超全超详细)
  2. 浅谈百度新一代query-ad 推荐引擎如何提升广告收益率
  3. iOS开发入门知识归纳
  4. python消费datahub_DataHub使用指南-阿里云开发者社区
  5. python随机取列表元素_python random从集合中随机选择元素
  6. 第二阶段个人工作总结04
  7. 三屏设置相关技术收集
  8. PHP TCPDF导出支持中文的pdf
  9. 使用GDI来绘制简易验证码
  10. 什么是生成式对抗神经网络GAN
  11. python docx官网_Python操作docx文档
  12. 2512 4.7K 5%贴片电阻识别方法
  13. Vue3 使用marked【代码高亮,安装使用教程】
  14. 好心情:当你被告知你患有抑郁症了,该怎么办
  15. 饥荒服务器显示队友mod,饥荒怎么显示物品给队友看 | 手游网游页游攻略大全
  16. 从事IT业一个8年老兵转行前的自我总结2——从《易经》说开来
  17. Java里鲜为人知的“机器人”类库
  18. Jxta 命令 shell
  19. Spring Cloud Netfilx Zuul : API网关服务
  20. MT6765开机LOGO图片的显示原理

热门文章

  1. puTTY的配置导入和导出
  2. Delphi XE2 之 FireMonkey 入门(2)
  3. POJ 1505(二分+贪心)
  4. poj-2115 C Looooops(扩展欧几里得)
  5. FZOJ 2014年11月份月赛 ytaaa(dp + RMQ)
  6. One-shot Learning with Memory-Augmented Neural Networks
  7. Python学习之路,第二次复习:列表,元组
  8. 编译时和运行时、OC中对象的动态编译机制
  9. 为何被主流抛弃-江西IDC机房价格为何居高不下缺少竞争力-2014年5月江西IDC排行榜...
  10. word-breakword-wrap