use master
gocreate procedure sp_who_lock
asbegin
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 @@error
insert 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>0
if @@error<>0 return @@error
-- 找到臨時表的記錄數
select @intcountproperties = count(*),@intcounter = 1
from #tmp_lock_who
if @@error<>0 return @@error
if @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_who
return 0
end殺死鎖和程序
如何去手動的殺死程序和鎖?最簡單的辦法,重新啟動服務。但是這裡要介紹乙個儲存過程,通過顯式的呼叫,可以殺死程序和鎖。
use master
goif exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[p_killspid]') and objectproperty(id, n'isprocedure') = 1)
drop procedure [dbo].[p_killspid]
gocreate proc p_killspid
@dbname varchar(200) --要關閉程序的資料庫名
as
declare @sql nvarchar(500)
declare @spid nvarchar(20)
declare #tb cursor for
select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id(@dbname)
open #tb
fetch next from #tb into @spid
while @@fetch_status=0
begin
exec('kill '+@spid)
fetch next from #tb into @spid
end
close #tb
deallocate #tb
go--用法
exec p_killspid 'newdbpy'
檢視鎖資訊
如何檢視系統中所有鎖的詳細資訊?在企業管理管理器中,我們可以看到一些程序和鎖的資訊,這裡介紹另外一種方法。
--檢視鎖資訊
create table #t(req_spid int,obj_name sysname)
declare @s nvarchar(4000)
,@rid int,@dbname sysname,@id int,@objname sysname
declare tb cursor for
select distinct req_spid,dbname=db_name(rsc_dbid),rsc_objid
from master..syslockinfo where rsc_type in(4,5)
open tb
fetch next from tb into @rid,@dbname,@id
while @@fetch_status=0
begin
set @s='select @objname=name from ['+@dbname+']..sysobjects where id=@id'
exec sp_executesql @s,n'@objname sysname out,@id int',@objname out,@id
insert into #t values(@rid,@objname)
fetch next from tb into @rid,@dbname,@id
endclose tb
deallocate tb
select 程序id=a.req_spid
,資料庫=db_name(rsc_dbid)
,型別=case rsc_type when 1 then 'null 資源(未使用)'
when 2 then '資料庫'
when 3 then '檔案'
when 4 then '索引'
when 5 then '表'
when 6 then '頁'
when 7 then '鍵'
when 8 then '擴充套件盤區'
when 9 then 'rid(行 id)'
when 10 then '應用程式'
end,物件id=rsc_objid
,物件名=b.obj_name
,rsc_indid
from master..syslockinfo a left join #t b on a.req_spid=b.req_spid
godrop table #t
找出較輕的那個球
description in user settings edit author your name date 2019 08 14 10 59 05 lastedittime 2019 08 14 16 47 34 lasteditors please set lasteditors 給定n個球,...
外來鍵造成的死鎖
在建表的過程中有部門,崗位,員工三個表,首先崗位表post的外來鍵department id對應部門department的department id欄位,可以說明這個崗位屬於哪個部門的,另外員工表employee的post id欄位對應post表的post id欄位,這樣可以找到該員工的崗位資訊,以...
間隙鎖的造成的死鎖
當我們用範圍條件而不是相等條件檢索資料,並請求共享或排他鎖時,innodb會給符合條件的已有資料記錄的索引項加鎖 對於鍵值在條件範圍內但並不存在的記錄,叫做 間隙 gap innodb也會對這個 間隙 加鎖,這種鎖機制就是所謂的間隙鎖 next key鎖 舉例來說,假如emp表中只有101條記錄,其...