网站首页 > 文章精选 正文
把查询方法打包成存储过程,需要用是执行存储过程就可以了:
CREATE procedure [dbo].[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 master..sysprocesses where blocked>0 ) a
where not exists(select * from (select * from master..sysprocesses where blocked>0 ) b
where a.blocked=spid)
union select spid,blocked from master..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
执行存储过程:
EXEC dbo.sp_who_lock
猜你喜欢
- 2025-01-10 最全MySQL锁详解(含悲观+乐观锁+行锁+表锁+页锁等)
- 2025-01-10 MySQL进阶篇SQL优化(InnoDB锁问题排查与解决)
- 2025-01-10 开发验证一切正常,而测试人员在性能测试时偶发报错,如何解决?
- 2025-01-10 建议收藏!看完这篇,你就理解数据库并发控制了(纯干货)
- 2025-01-10 高并发场景下的MySQL几类死锁事故案例分析
- 2025-01-10 浅谈数据库锁机制
- 2025-01-10 大厂面试题:千万级别mysql表锁表了怎么办,如何处理
- 2025-01-10 java服务程序假死(进程存在但请求无响应)的几种原因
- 2025-01-10 服了!Delete同一行记录也会造成死锁?!
- 2025-01-10 「案例回放」线上环境一次真实的MySQL死锁案例
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- react官网 (408)
- esd文件 (378)
- 更新目录 (379)
- 数据抓取 (373)
- pip换源 (412)
- display:none (369)
- img文件怎么打开 (475)
- a标签怎么去掉下划线 (376)
- git拉取代码 (435)
- 图片代码 (411)
- user-select (415)
- 访问github (415)
- 服务主机本地系统cpu占用高 (401)
- e.target (437)
- pycharm主题 (395)
- 火狐浏览器插件 (408)
- file.exists (413)
- js文件 (425)
- ip更换 (389)
- mssql和mysql区别 (366)
- 755权限 (389)
- requesttimeout (384)
- mysql默认密码 (398)
- pcm文件 (387)
- ipython和python区别 (387)
- 最新留言
-
本文暂时没有评论,来添加一个吧(●'◡'●)