/*
-- 处理死锁
-- 查看当前进程,或死锁进程,并能自动杀掉死进程
-- 因为是针对死的,所以如果有死锁进程,只能查看死锁进程
-- 当然,你可以通过参数控制,不管有没有死锁,都只查看死锁进程
-- 调用示例:
exec [dbo].[p_lockinfo] 0, 0
*/
CREATE PROCEDURE [dbo].[p_lockinfo]
@kill_lock_spid bit = 0, --是否杀掉死锁的进程,1 杀掉, 0 仅显示
@show_spid_if_nolock bit = 0 --如果没有死锁的进程,是否显示正常进程信息,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(4000));
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;
--print @s;
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 order by a.id;
end GO

sqlserver查看死锁进程工具脚本p_lockinfo的更多相关文章

  1. SQLServer查看死锁

    SQLServer查看死锁 if exists ( select * from sys.procedures where name like '%USP_ShowLocks%' ) drop proc ...

  2. SqlServer 查看死锁的进程

    版权声明:本文为 走错路的程序员 原创文章,欢迎转载. https://blog.csdn.net/phker/article/details/78612974搞sqlsever 好多年, 从来没锁过 ...

  3. 【ORACLE】查看死锁进程并结束死锁的脚本

    --共享锁:Share:排他锁:Exclusive:行共享锁:Row-S:行排他锁:Row-X select V$SESSION.sid,v$session.SERIAL#,v$process.spi ...

  4. Sql Server查看死锁及堵塞脚本

    --每秒死锁数量 SELECT * FROM sys.dm_os_performance_counters WHERE counter_name LIKE 'Number of Deadlocksc% ...

  5. SQLServer查看和解决死锁的方法

    http://luohonghong.blog.163.com/blog/static/78312058201142411533316/ SQLServer查看和解决死锁的方法 2011-05-24 ...

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

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

  7. 【2018-01-26】SqlServer 检查死锁和阻塞

    利用sys.sysprocesses SQL进程检查是否出现死锁和阻塞 Sys.SysProcesses 系统表是一个很重要的系统视图,主要用来定位与解决Sql Server的阻塞和死锁 select ...

  8. sqlserver检测死锁;杀死锁和进程;查看锁信息

    http://blog.sina.com.cn/s/blog_9dcdd2020101nf4v.html sqlserver检测死锁;杀死锁和进程;查看锁信息 ( ::)转载▼ 标签: sql 检测死 ...

  9. sql server 查看表的死锁和Kill 死锁进程

    查询出来 select        request_session_id spid,       OBJECT_NAME(resource_associated_entity_id) tableNa ...

随机推荐

  1. git stash暂存当前正在进行的工作

    git stash 可用来暂存当前正在进行的工作, 比如想pull 最新代码, 又不想加新commit, 或者另外一种情况,为了fix 一个紧急的bug,  先stash, 使返回到自己上一个comm ...

  2. Spring使用_进阶

    概述 本文主要写了几个关于Spring Aware,多线程,计划任务(定时任务),条件注解,组合注解,元注解,Spring测试的小例子以及关于@Enable*注解的工作原理的理解. Spring Aw ...

  3. 移动端REM布局模板(阿里高清方案)

    移动端REM布局模板(阿里高清方案),蛮好的,转自: http://www.jianshu.com/p/985d26b40199 . <!DOCTYPE html> <html la ...

  4. 浅析ES6中的iterator

    1.iterator迭代器必须保证其遍历终止条件可控,否则会形成死循环demo: //会用到iterator接口的场合 //1.for...of循环 //2. ...解构表达式 const obj = ...

  5. Android 坑爹问题

    A/art: art/runtime/jdwp/jdwp_event.cc:] Check failed: Thread::Current() != GetDebugThread() (Thread: ...

  6. cacti添加被监控机全过程

    在被监控端上的操作: 1.在被监控机器上root目录下建立文件 test.sh chmod 777 test.sh cat test #!/bin/bash echo $RANDOM 2.在snmpd ...

  7. nginx 编译某个模板的问题./configure: error: SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library stati

    root@hett-PowerEdge-T30:/usr/local/src/nginx-1.9.8# ./configure --prefix=/usr/local/nginx  --add-mod ...

  8. python基础一 day11 装饰器(1)

    接收的时候是聚合,调用的时候是打散     print(*args)本来在里面用的时候是用args,是一个元祖,加上一个 * 号,把元祖解包了(打散了). from functools import ...

  9. softmax_loss

    softmax_loss中的ignore_label是来自于loss layer,而不是softmax_loss的参数

  10. python之道06

    1,使⽤循环打印以结果: * *** ***** ******* ********* 答案: 方法一: for i in range(10): if i % 2 == 1: print(i*'*') ...