-- exec [dbo].[ht_who_lockex] 1 --- dbc
create procedure [dbo].[ht_who_lockex]
@iskill as bit -- 是否自動刪除死鎖聯接
asbegin
declare @spid int,@bl int,
@inttransactioncountonentry int,
@introwcount int,
@intcountproperties int,
@intcounter int
create table #tmp_lock_sql (
eventtype varchar(200),
parameters varchar(200),
eventinfo varchar(5000))
create table #tmp_lock_who (
id int identity(1,1),
spid smallint,
bl smallint,
last_batch datetime)
if @@error<>0
select @@error
insert into #tmp_lock_who(spid,bl,last_batch)
select 0 as spid,blocked as bl,last_batch from (select * from master.dbo.sysprocesses where blocked>0 ) a
where not exists
(select * from (select * from master.dbo.sysprocesses where blocked>0 ) b
where a.blocked=spid)
union select spid,blocked,last_batch from master.dbo.sysprocesses where blocked>0
if @@error<>0
select @@error
-- 找到臨時表的記錄數
select @intcountproperties = count(*),@intcounter = 1 from #tmp_lock_who
if @@error<>0
select @@error
if @intcountproperties=0
select '現在沒有阻塞和死鎖資訊' as message
-- 迴圈開始
declare @lastdate as datetime
while @intcounter <= @intcountproperties
begin
-- 取第一條記錄
select * from [master].[dbo].[sysprocesses] where spid = @bl
select @spid = spid,@bl = bl,@lastdate= last_batch
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 )
truncate table #tmp_lock_sql
insert into #tmp_lock_sql(eventtype,parameters,eventinfo)
exec('dbcc inputbuffer ('+@bl+')')
select '耗時:'+cast(datediff(ss,@lastdate,getdate()) as varchar(20))+'秒' + case when datediff(ss,@lastdate,getdate())>20 and @spid=0 then ' 已執行kill' else '' end as 提示, * from #tmp_lock_sql
declare @icsqlstring as varchar(max)
if @iskill=1 and datediff(ss,@lastdate,getdate())>20
begin
set @icsqlstring = 'kill ' + cast(@bl as varchar(12))
exec (@icsqlstring)
endend
-- 迴圈指標下移
set @intcounter = @intcounter + 1
endselect spid as id
,lastwaittype as 等待型別
,cpu as cpu
,login_time as 登入時間
,last_batch as 最後處理
,status as 狀態
,cmd as 命令型別
,loginame as 登入使用者
,hostname as 登入終端
,program_name as 執行程式
from
[master].[dbo].[sysprocesses] where 1=1 and not status in ('sleeping','background')
order by 狀態,cpu desc
drop table #tmp_lock_who
return 0
end
查詢死鎖 和 解決死鎖
sql中執行 sp who lock kill 1 1 語句中查詢出來的id set ansi nulls on go set quoted identifier on go alter procedure dbo sp who lock as begin declare spid int decl...
查詢死鎖的方法
死鎖主要是由於操作不當導致執行緒之間出現相互等待,一般有source code的和pdb就可以找到死鎖的原因,只要在code中不出現terminatethread,一般均可找到原因。若使用了第三方庫,別人的code就不在我們的管控範圍以內,很難定位到具體原因。一般可以看到ntdll的臨界區被占用,r...
mysql 查詢死鎖sql
1.檢視程序 show processlist 2.檢視是否鎖表 show open tables where in use 0 3.檢視正在鎖的事務 select from information schema.innodb locks 4.檢視等待鎖的事務 select from informa...