查看死锁的存储过程

CREATE procedure 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
时间: 2024-11-03 21:45:36

查看死锁的存储过程的相关文章

SQL Server 查看死锁的存储过程(转载)

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_who_lock]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_who_lock] GO use master go create procedure sp_who_lock as begin declare @spid int,@bl int, @int

oracle查看死锁与解锁

1.查看死锁 SELECT  s.username, decode(l.type,'TM','TABLE LOCK', 'TX','ROW LOCK', NULL) LOCK_LEVEL, o.owner,o.object_name,o.object_type, s.sid,s.serial#,s.terminal,s.machine,s.program,s.osuser FROM v$session s,v$lock l,dba_objects o WHERE l.sid = s.sid AN

sqlserver 查看进程以及查看死锁代码

查看进程: SELECT spid, blocked, DB_NAME(sp.dbid) AS DBName, program_name, waitresource, lastwaittype, sp.loginame, sp.hostname, a.[Text] AS [TextData], SUBSTRING(A.text, sp.stmt_start / 2, (CASE WHEN sp.stmt_end = -1 THEN DATALENGTH(A.text) ELSE sp.stmt_

SQLSERVER查询数据库死锁的存储过程

原文:SQLSERVER查询数据库死锁的存储过程 USE [IdentityDemo] GO /****** Object: StoredProcedure [dbo].[sp_who_lock] Script Date: 2019/1/17 10:47:53 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE procedure [dbo].[sp_who_lock] as begin declare @spid in

oracle中“ORA-00060: 等待资源时检测到死锁” 或存储过程编译卡死 解决方法

之前在调试存储过程时,出现卡死情况,无法插入数据 解决方法 1.查看那些表被锁住: --1.查看那些表被锁住--- select b.owner,b.object_name,a.session_id,a.locked_mode from v$locked_object a,dba_objects b where b.object_id = a.object_id; 2.查看引起死锁的会话: --3.查看引起死锁会话-- select b.username,b.sid,b.serial#,logo

查看Oracle中存储过程长时间被卡住的原因

1:查V$DB_OBJECT_CACHE SELECT * FROM V$DB_OBJECT_CACHE WHERE name='CUX_OE_ORDER_RPT_PKG' AND LOCKS!='0'; 注意:CUX_OE_ORDER_RPT_PKG 为存储过程的名称. 发现 locks=2 2:按对象查出sid的值 select /*+ rule*/  SID from V$ACCESS WHERE object='CUX_OE_ORDER_RPT_PKG'; 注意:CUX_OE_ORDER

sql server查看有哪些存储过程和视图的方法

select * from sys.sysobjects where type='p'  //存储过程 select * from sys.sysobjects where type='v'  //视图 sp_helptext procName //查看存储过程结构 sp_helptext vwName //查看视图结构

SqlServer查看表、存储过程、耗时查询、当前进程、开销较大的语句

--查看数据库中表的语句 SELECT s2.dbid , DB_NAME(s2.dbid) AS [数据库名] , --s1.sql_handle , ( SELECT TOP 1 SUBSTRING(s2.text, statement_start_offset / 2 + 1, ( ( CASE WHEN statement_end_offset = -1 THEN ( LEN(CONVERT(NVARCHAR(MAX), s2.text)) * 2 ) ELSE statement_en

Java 查看死锁的方法

那我们怎么确定一定是死锁呢?有两种方法. 1>使用JDK给我们的的工具JConsole,可以通过打开cmd然后输入jconsole打开. 1)连接到需要查看的进程. 2)打开线程选项卡,然后点击左下角的“检测死锁” 3)jconsole就会给我们检测出该线程中造成死锁的线程,点击选中即可查看详情: 从上图中我们可以看出: ①在线程Thread-1中,从状态可以看出,它想申请Paper这个资源,但是这个资源已经被Thread-0拥有了,所以就堵塞了. ②在线程Thread-0中,从状态可以看出,它