解决SQL Server数据库表死锁【亲测有效】

 时间:2026-02-12 09:05:42

1、打开Microsoft SQL Server Management Studio管理工具,新建查询窗口并复制运行如下命令行以创建获取数据库当前死锁进程的ID和死锁进程执行SQL语句信息的存储过程,

--查看当前死锁进程SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO

create procedure deadlockas 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 @@ERRORinsert 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>0IF @@ERROR<>0 RETURN @@ERROR   -- 找到临时表的记录数 select @intCountProperties = Count(*),@intCounter = 1 from #tmp_lock_whoIF @@ERROR<>0 RETURN @@ERRORif @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 enddrop table #tmp_lock_whoreturn 0 end GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO

2、打开查询分析器,执行存储过程deadlock,默认数据库为master,执行完成即可显示死锁的进程ID和操作死锁的SQL语句。

3、在查询分析器中通过运行命令行终止死锁的进程,再进行表的索引重建和重新组织即可恢复正常。

--终止死锁进程ID

kill 死锁进程ID

  • 电脑老是蓝屏怎么修复
  • win11文件共享打不开解决方法
  • win10电脑为什么关机后又会自动重启
  • 联想启天B4550-B031电脑不能安装windows XP系统
  • 远程桌面连接命令怎么用
  • 热门搜索
    魍魉怎么读 冰箱密封条不严怎么办 手机一直重启怎么办 qq头像怎么换 东风风光580怎么样 美的中央空调怎么样 开根号怎么算 怎么治疗湿疹 手汗多怎么办 有番号怎么下载