当前位置:Gxlcms > mssql > SqlServer查询和Kill进程死锁的语句

SqlServer查询和Kill进程死锁的语句

时间:2021-07-01 10:21:17 帮助过:68人阅读

查询死锁进程语句

  1. select
  2. request_session_id spid,
  3. OBJECT_NAME(resource_associated_entity_id) tableName
  4. from
  5. sys.dm_tran_locks
  6. where
  7. resource_type='OBJECT'

杀死死锁进程语句

  1. kill spid

下面再给大家分享一段关于sqlserver检测死锁;杀死锁和进程;查看锁信息

  1. --检测死锁
  2. --如果发生死锁了,我们怎么去检测具体发生死锁的是哪条SQL语句或存储过程?
  3. --这时我们可以使用以下存储过程来检测,就可以查出引起死锁的进程和SQL语句。SQL Server自带的系统存储过程sp_who和sp_lock也可以用来查找阻塞和死锁, 但没有这里介绍的方法好用。
  4. use master
  5. go
  6. create procedure sp_who_lock
  7. as
  8. begin
  9. declare @spid int,@bl int,
  10. @intTransactionCountOnEntry int,
  11. @intRowcount int,
  12. @intCountProperties int,
  13. @intCounter int
  14. create table #tmp_lock_who (
  15. id int identity(1,1),
  16. spid smallint,
  17. bl smallint)
  18. IF @@ERROR<>0 RETURN @@ERROR
  19. insert into #tmp_lock_who(spid,bl) select 0 ,blocked
  20. from (select * from sysprocesses where blocked>0 ) a
  21. where not exists(select * from (select * from sysprocesses where blocked>0 ) b
  22. where a.blocked=spid)
  23. union select spid,blocked from sysprocesses where blocked>0
  24. IF @@ERROR<>0 RETURN @@ERROR
  25. -- 找到临时表的记录数
  26. select @intCountProperties = Count(*),@intCounter = 1
  27. from #tmp_lock_who
  28. IF @@ERROR<>0 RETURN @@ERROR
  29. if @intCountProperties=0
  30. select '现在没有阻塞和死锁信息' as message
  31. -- 循环开始
  32. while @intCounter <= @intCountProperties
  33. begin
  34. -- 取第一条记录
  35. select @spid = spid,@bl = bl
  36. from #tmp_lock_who where Id = @intCounter
  37. begin
  38. if @spid =0
  39. select '引起数据库死锁的是: '+ CAST(@bl AS VARCHAR(10)) + '进程号,其执行的SQL语法如下'
  40. else
  41. select '进程号SPID:'+ CAST(@spid AS VARCHAR(10))+ '被' + '进程号SPID:'+ CAST(@bl AS VARCHAR(10)) +'阻塞,其当前进程执行的SQL语法如下'
  42. DBCC INPUTBUFFER (@bl )
  43. end
  44. -- 循环指针下移
  45. set @intCounter = @intCounter + 1
  46. end
  47. drop table #tmp_lock_who
  48. return 0
  49. end
  50. --杀死锁和进程
  51. --如何去手动的杀死进程和锁?最简单的办法,重新启动服务。但是这里要介绍一个存储过程,通过显式的调用,可以杀死进程和锁。
  52. use master
  53. go
  54. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_killspid]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
  55. drop procedure [dbo].[p_killspid]
  56. GO
  57. create proc p_killspid
  58. @dbname varchar(200) --要关闭进程的数据库名
  59. as
  60. declare @sql nvarchar(500)
  61. declare @spid nvarchar(20)
  62. declare #tb cursor for
  63. select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id(@dbname)
  64. open #tb
  65. fetch next from #tb into @spid
  66. while @@fetch_status=0
  67. begin
  68. exec('kill '+@spid)
  69. fetch next from #tb into @spid
  70. end
  71. close #tb
  72. deallocate #tb
  73. go
  74. --用法
  75. exec p_killspid 'newdbpy'
  76. --查看锁信息
  77. --如何查看系统中所有锁的详细信息?在企业管理管理器中,我们可以看到一些进程和锁的信息,这里介绍另外一种方法。
  78. --查看锁信息
  79. create table #t(req_spid int,obj_name sysname)
  80. declare @s nvarchar(4000)
  81. ,@rid int,@dbname sysname,@id int,@objname sysname
  82. declare tb cursor for
  83. select distinct req_spid,dbname=db_name(rsc_dbid),rsc_objid
  84. from master..syslockinfo where rsc_type in(4,5)
  85. open tb
  86. fetch next from tb into @rid,@dbname,@id
  87. while @@fetch_status=0
  88. begin
  89. set @s='select @objname=name from ['+@dbname+']..sysobjects where id=@id'
  90. exec sp_executesql @s,N'@objname sysname out,@id int',@objname out,@id
  91. insert into #t values(@rid,@objname)
  92. fetch next from tb into @rid,@dbname,@id
  93. end
  94. close tb
  95. deallocate tb
  96. select 进程id=a.req_spid
  97. ,数据库=db_name(rsc_dbid)
  98. ,类型=case rsc_type when 1 then 'NULL 资源(未使用)'
  99. when 2 then '数据库'
  100. when 3 then '文件'
  101. when 4 then '索引'
  102. when 5 then '表'
  103. when 6 then '页'
  104. when 7 then '键'
  105. when 8 then '扩展盘区'
  106. when 9 then 'RID(行 ID)'
  107. when 10 then '应用程序'
  108. end
  109. ,对象id=rsc_objid
  110. ,对象名=b.obj_name
  111. ,rsc_indid
  112. from master..syslockinfo a left join #t b on a.req_spid=b.req_spid
  113. go
  114. drop table #t

以上所述是小编给大家介绍的SqlServer查询和Kill进程死锁的语句,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

您可能感兴趣的文章:

  • sql server中死锁排查的全过程分享
  • 详解SQL Server中的事务与锁问题
  • sqlserver:查询锁住sql以及解锁方法
  • SQL Server学习笔记之事务、锁定、阻塞、死锁用法详解
  • Sql Server 死锁的监控分析解决思路
  • Sql Server如何查看被锁的表及解锁的方法
  • sqlserver进程死锁关闭的方法
  • sqlserver锁表、解锁、查看销表的方法
  • 查找sqlserver查询死锁源头的方法 sqlserver死锁监控
  • SQL Server三种锁定模式的知识讲解

人气教程排行