查询有没有死锁的存储过程

USE [erpdb1]
GO
/****** Object:  StoredProcedure [dbo].[p_lockinfo_My]    Script Date: 12/26/2014 14:58:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO

ALTER proc [dbo].[p_lockinfo_My]
@kill_lock_spid bit=0,  --是否杀掉死锁的进程,1 杀掉, 0 仅显示
@show_spid_if_nolock bit=1 --如果没有死锁的进程,是否显示正常进程信息,1 显示,0 不显示
as
declare @count int,@s nvarchar(1000),@i int
select id=identity(int,1,1),标志,
 进程ID=spid,线程ID=kpid,块进程ID=blocked,数据库ID=dbid,
 数据库名=db_name(dbid),用户ID=uid,用户名=loginame,累计CPU时间=cpu,
 登陆时间=login_time,打开事务数=open_tran, 进程状态=status,
 工作站名=hostname,应用程序名=program_name,工作站进程ID=hostprocess,
 域名=nt_domain,网卡地址=net_address
into #t from(
 select 标志=‘死锁的进程‘,
  spid,kpid,a.blocked,dbid,uid,loginame,cpu,login_time,open_tran,
  status,hostname,program_name,hostprocess,nt_domain,net_address,
  s1=a.spid,s2=0
 from master..sysprocesses a join (
  select blocked from master..sysprocesses group by blocked
  )b on a.spid=b.blocked where a.blocked=0
 union all
 select ‘|_牺牲品_>‘,
  spid,kpid,blocked,dbid,uid,loginame,cpu,login_time,open_tran,
  status,hostname,program_name,hostprocess,nt_domain,net_address,
  s1=blocked,s2=1
 from master..sysprocesses a where blocked<>0
)a order by s1,s2

select @[email protected]@rowcount,@i=1

if @count=0 and @show_spid_if_nolock=1
begin
 insert #t
 select 标志=‘正常的进程‘,
  spid,kpid,blocked,dbid,db_name(dbid),uid,loginame,cpu,login_time,
  open_tran,status,hostname,program_name,hostprocess,nt_domain,net_address
 from master..sysprocesses
 set @[email protected]@rowcount
end

if @count>0
begin
 create table #t1(id int identity(1,1),a nvarchar(30),b Int,EventInfo nvarchar(255))
 if @kill_lock_spid=1
 begin
  declare @spid varchar(10),@标志 varchar(10)
  while @i<[email protected]
  begin
   select @spid=进程ID,@标志=标志 from #t where [email protected]
   insert #t1 exec(‘dbcc inputbuffer(‘[email protected]+‘)‘)
   if @标志=‘死锁的进程‘ exec(‘kill ‘[email protected])
   set @[email protected]+1
  end
 end
 else
  while @i<[email protected]
  begin
   select @s=‘dbcc inputbuffer(‘+cast(进程ID as varchar)+‘)‘ from #t where [email protected]
   insert #t1 exec(@s)
   set @[email protected]+1
  end
 select a.*,进程的SQL语句=b.EventInfo
 from #t a join #t1 b on a.id=b.id
end

时间: 2024-11-08 10:33:27

查询有没有死锁的存储过程的相关文章

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

如何在SQL Server查询语句(Select)中检索存储过程(Store Procedure)的结果集?

如何在SQL Server查询语句(Select)中检索存储过程(Store Procedure)的结果集?(2006-12-14 09:25:36) 与这个问题具有相同性质的其他描述还包括:如何在SQL Server存储过程中获取另一存储过程的执行结果记录集?如何在存储过程中检索动态SQL语句的执行结果?如何实现类似SELECT * FROM (EXEC procedure_name @parameters_var) AS datasource ... 的功能?procedure_

查询sqlserver数据库视图、存储过程等包含特定的字符串

? 1 2 3 4 5 6 SELECT  A.name ,         B.definition FROM    SYS.objects A         INNER JOIN sys.sql_modules B ON A.object_id = B.object_id                                         AND ( CHARINDEX('包含字符',                                               

SQL 查询某个表被哪些存储过程使用到

--1.查询某个表被哪些存储过程使用到 : select distinct object_name(id) from syscomments where id in (select object_id from sys.objects where type ='P') and text like'%TableName%' --2.查找那些过程对该表做了更新操作: select distinct object_name(id) from syscomments where id in(select

SQL server 查询某个表在哪些存储过程(SP)中使用到

1.查询某个表被哪些存储过程(以下简称 SP)使用到 : select distinct object_name(id) from syscomments where id in (select id from sysobjects where type ='P') and text like'%TableName%' 2.查找那些过程对该表做了更新操作: select distinct object_name(id) from syscomments where id in (select i

sql server 查询某个表被哪些存储过程调用

原文:sql server 查询某个表被哪些存储过程调用 sql server 查询某个表被哪些存储过程调用 select distinct object_name(id) from syscomments where id in (select id from sysobjects where type ='P') and text like'%TableName%' 原文地址:https://www.cnblogs.com/lonelyxmas/p/9491635.html

查询某个表被那些存储过程用到

--sql2008 查询某个表被那些存储过程用到select distinct object_name(id) from syscommentswhere id in (select object_id from sys.objects where type ='P')and  UPPER(text) like '%YN-C-1803%' order by object_name(id)/*type char(2) 对象类型.可以是下列值之一: C = CHECK 约束 D = 默认值或 DEF

查询Sqlserver数据库死锁的一个存储过程

使用sqlserver作为数据库的应用系统,都避免不了有时候会产生死锁, 死锁出现以后,维护人员或者开发人员大多只会通过sp_who来查找死锁的进程,然后用sp_kill杀掉.利用sp_who_lock这个存储过程,可以很方便的知道哪个进程出现了死锁,出现死锁的问题在哪里. 创建sp_who_lock存储过程 CREATE procedure sp_who_lock   as      begin         declare @spid int         declare @blk in

Hibernate(十三)命名查询-批量处理和调用存储过程

一.命名查询 HQL语句混杂在代码之间将破坏代码的可读性 Hibernate允许在映射配置文件中定义字符串形式的查询语句,这种查询方式称为命名查询 二.建立数据表和持久化类 建立数据表 create table login ( username varchar2(32) primary key, password varchar2(32) not null , age number(3) ); insert into login select '张三','123456',21 from dual