惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
博客园 - 【当耐特】
S
SegmentFault 最新的问题
阮一峰的网络日志
阮一峰的网络日志
大猫的无限游戏
大猫的无限游戏
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
Martin Fowler
Martin Fowler
云风的 BLOG
云风的 BLOG
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Fortinet All Blogs
Y
Y Combinator Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
J
Java Code Geeks
Engineering at Meta
Engineering at Meta
MyScale Blog
MyScale Blog
B
Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
人人都是产品经理
人人都是产品经理

博客园 - 一味

[翻译]实例:在Android调用WCF服务 基于LRU淘汰的高性能缓存 不是架构的架构之五:业务层的实现与自动代理(补充) 不是架构的架构之四:业务层的实现与自动代理 不是架构的架构之三:系统基础(2)主键选择和并发 不是架构的架构之二:系统基础(1) 不是架构的架构之一:总体思路 一道算法题 技术先行or业务先行 用了一年的键盘,记录下键盘磨损的状况 一个业务系统设计构想(一) 安装VS2008/.Net3.5/.Net3.0/.Net2.0sp1失败的解决办法 常用存储过程4(K310。3版本获取物流新单据的编码) 常用存储过程3(获取编码的上级编码和短编码) 常用存储过程2(获取编码级次) 常用存储过程1(获取字符串中的第一个数值) SQL Server中Rollup关键字使用技巧 Math.Round函数四舍五入的问题 学习NHibernate的感悟和疑惑
SQL Server进程阻塞的检查和解决办法(转自好友Blog)
一味 · 2006-06-29 · via 博客园 - 一味

先声明,这篇文章是转自一个好友(文心残)的Blog。
我最近在项目中遇到了这样的问题,这篇文章给了一个比较好的解决方案。

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
GO


--结束SQL阻塞的进程%%%%%%%%%%%%%%%%%%%%%%
create    procedure sp_Kill_lockProcess
as
begin
        
SET NOCOUNT ON
 
declare @spid int,@bl int,
 
@intTransactionCountOnEntry int,
 
@intRowcount int,
 
@intCountProperties int,
 
@intCounter int,
        
@sSql nvarchar (200)
 
 
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
 
 
while @intCounter <= @intCountProperties
 
begin
         
-- 取第一条记录
  select @spid = spid,@bl = bl
  
from #tmp_lock_who where Id = @intCounter
  
begin
   
if @spid =0
   
begin
                          
set @sSql='kill ' + CAST(@bl AS VARCHAR(10))
                          
exec sp_executesql @sSql
   
end
  
end
  
  
-- 循环指针下移
  set @intCounter = @intCounter + 1
 
end
 
 
drop table #tmp_lock_who
        
SET NOCOUNT OFF
 
return 0
end

GO