USE MyDataBase
GO /****** Object: StoredProcedure [dbo].[sp_who_lock] Script Date: 4/10/2015 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[sp_who_lock]
as
begin
declare @spid int
declare @blk int
declare @count int
declare @index int
declare @lock tinyint
set @lock=0
create table #temp_who_lock
(
id int identity(1,1),
spid int,
blk int
)
if @@error<>0 return @@error
insert into #temp_who_lock(spid,blk)
select 0 ,blocked
from (select * from master..sysprocesses where blocked>0)a
where not exists(select * from master..sysprocesses where a.blocked =spid and blocked>0)
union select spid,blocked from master..sysprocesses where blocked>0
if @@error<>0 return @@error
select @count=count(*),@index=1 from #temp_who_lock
if @@error<>0 return @@error
if @count=0
begin
select '没有阻塞和死锁信息'
return 0
end
while @index<=@count
begin
if exists(select 1 from #temp_who_lock a where id>@index and exists(select 1 from #temp_who_lock where id<=@index and a.blk=spid))
begin
set @lock=1
select @spid=spid,@blk=blk from #temp_who_lock where id=@index
select '引起数据库死锁的是: '+ CAST(@spid AS VARCHAR(10)) + '进程号,其执行的SQL语法如下'
select @spid, @blk
dbcc inputbuffer(@spid)
dbcc inputbuffer(@blk)
end
set @index=@index+1
end
if @lock=0
begin
set @index=1
while @index<=@count
begin
select @spid=spid,@blk=blk from #temp_who_lock where id=@index
if @spid=0
select '引起阻塞的是:'+cast(@blk as varchar(10))+ '进程号,其执行的SQL语法如下'
else
select '进程号SPID:'+ CAST(@spid AS VARCHAR(10))+ '被' + '进程号SPID:'+ CAST(@blk AS VARCHAR(10)) +'阻塞,其当前进程执行的SQL语法如下'
dbcc inputbuffer(@spid)
dbcc inputbuffer(@blk)
set @index=@index+1
end
end
drop table #temp_who_lock
return 0
end
GO

sp_who_lock的更多相关文章

  1. 查看死锁 的存储过程 ,工具存储过程 sp_who_lock

    http://blog.sina.com.cn/s/blog_95b5eb8c010162jp.html if exists (select * from dbo.sysobjects where i ...

  2. SQLSERVER——查看阻塞信息(sp_who_lock优化无误版)

    经常会需要分析SQLSERVER的阻塞情况,尤其是某些SQL操作异常缓慢从而怀疑是有人在搞事情的情况下.网上有许多一模一样的帖子,是关于sp_who_lock这个存储过程的,然而,网上流传的这个是略有 ...

  3. SQL Server中的事务与锁

    了解事务和锁 事务:保持逻辑数据一致性与可恢复性,必不可少的利器. 锁:多用户访问同一数据库资源时,对访问的先后次序权限管理的一种机制,没有他事务或许将会一塌糊涂,不能保证数据的安全正确读写. 死锁: ...

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

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

  5. SQL Server 监控统计阻塞脚本信息

        数据库产生阻塞(Blocking)的本质原因 :SQL语句连续持有锁的时间过长 ,数目过多, 粒度过大.阻塞是事务隔离带来的副作用,它是不可避免的,而且是一个数据库系统常见的现象. 但是阻塞的 ...

  6. MS SQL 日常维护管理常用脚本(二)

    监控数据库运行 下面是整理.收集监控数据库运行的一些常用脚本,也是MS SQL 日常维护管理常用脚本(一)的续集,欢迎大家补充.提意见. 查看数据库登录名信息   Code Snippet SELEC ...

  7. SQL Server 数据库巡检脚本

    --1.查看数据库版本信息 select @@version --2.查看所有数据库名称及大小 exec sp_helpdb --3.查看数据库所在机器的操作系统参数 exec master..xp_ ...

  8. 查询Sqlserver数据库死锁的一个存储过程

    From:http://www.cnblogs.com/mzhanker/archive/2011/06/04/2072739.html 使用sqlserver作为数据库的应用系统,都避免不了有时候会 ...

  9. SQL超过锁请求

    ---超过锁请求 http://blog.sina.com.cn/s/blog_7fb3b1840100u4dj.html 1.查询造成死锁的SQL语句 当SQL数据库的监控报警显示死锁进程数过多时, ...

随机推荐

  1. 解决Oracle+weblogic系统死机的问题

    前段时间发布的系统(Oracle+weblogic)频繁挂掉,每天早上9点.下午2点高峰期就挂,纠结了很长时间,最终解决,方法描述下. 执行select count(*),status from v$ ...

  2. JAVA 布尔型的应用

    定义一个布尔类型的flag, 只是当做一个开关的意思.先定义一个标记,然后根据一些条件给这个flag赋不同的值,最后,再根据这个flag不同的值,做不同的处理.   public static voi ...

  3. Shell 编写准则

    Shell 中备注符号: # 或者 " 作为Shell中的备注符号. 需要注意的是,首行的#!/bin/bash 不是注释 Shell 格式 首行输入  #!/bin/bash 中间输入运行 ...

  4. yii2.0邮箱发送

    邮件发送配置: 打开配置文件将下面代码添加到 components => [...]中(例:高级版默认配置在/common/config/main-local.php)         'mai ...

  5. Singleton Design Pattern

    The Singleton pattern is one of the simplest design patterns, which restricts the instantiation of a ...

  6. 【LeetCode OJ】Recover Binary Search Tree

    Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...

  7. dreamweaver快捷键

    ---恢复内容开始--- 件菜单 新建文档 Ctrl+N 打开一个 HTML文件 Ctrl+O或者将文件从[文件管理器]或[站点]窗口拖动到[文档]窗口中 在框架中打开 Ctrl+Shift+O 关闭 ...

  8. 【Mocha.js 101】钩子函数

    前情提要 在上一篇文章<[Mocha.js 101]同步.异步与 Promise>中,我们学会了如何对同步方法.异步回调方法以及 Promise 进行测试. 在本篇文章中,我们将了解到 M ...

  9. IC卡复位应答ATR解析

    输入的是ATR,通过解析输出TA.TB.TC.TD的信息. 似乎没有容错处理,~~~~(>_<)~~~~ #include <stdio.h> #define TA_BIT ( ...

  10. 1.Counting DNA Nucleotides

    Problem A string is simply an ordered collection of symbols selected from some alphabet and formed i ...