SqlServer查詢和Kill過程逝世鎖的語句。本站提示廣大學習愛好者:(SqlServer查詢和Kill過程逝世鎖的語句)文章只能為提供參考,不一定能成為您想要的結果。以下是SqlServer查詢和Kill過程逝世鎖的語句正文
查詢逝世鎖過程語句
select request_session_id spid, OBJECT_NAME(resource_associated_entity_id) tableName from sys.dm_tran_locks where resource_type='OBJECT'
殺逝世逝世鎖過程語句
kill spid
上面再給年夜家分享一段關於sqlserver檢測逝世鎖;殺逝世鎖和過程;檢查鎖信息
--檢測逝世鎖
--假如產生逝世鎖了,我們怎樣去檢測詳細產生逝世鎖的是哪條SQL語句或存儲進程?
--這時候我們可使用以下存儲進程來檢測,便可以查出惹起逝世鎖的過程和SQL語句。SQL Server自帶的體系存儲進程sp_who和sp_lock也能夠用來查找壅塞和逝世鎖, 但沒有這裡引見的辦法好用。
use master
go
create procedure sp_who_lock
as
begin
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
end
drop table #tmp_lock_who
return 0
end
--殺逝世鎖和過程
--若何去手動的殺逝世過程和鎖?最簡略的方法,從新啟動辦事。然則這裡要引見一個存儲進程,經由過程顯式的挪用,可以殺逝世過程和鎖。
use master
go
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_killspid]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_killspid]
GO
create 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
end
close 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
go
drop table #t
以上所述是小編給年夜家引見的SqlServer查詢和Kill過程逝世鎖的語句,願望對年夜家有所贊助,假如年夜家有任何疑問請給我留言,小編會實時答復年夜家的。在此也異常感激年夜家對網站的支撐!