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
declare @blk int
declare @count int
declare @index int
declare @lock tinyint 、firefox
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
死鎖與解決死鎖
要是以後寫資料庫的產品,不是寫sql語句,那裡面涉及到的鎖就跟多了。故事裡有五個哲學家 這些哲學家很窮,只買得起五根筷子。他們坐成一圈,兩個人的中間放一根筷子。哲學家吃飯的時候必須同時得到左手邊和右手邊的筷子。如果他身邊的任何一位正在使用筷子,那他只有等著。假設哲學家的編號是a b c d e,筷子...
oracle解決死鎖
第一步 檢視是否有死鎖存在,查出有資料則代表有死鎖 select p.spid,c.object name,b.session id,b.oracle username,b.os user name from v process p,v session a,v locked object b,all...
Windows Oracle解決死鎖
背景 執行某個查詢功能,無返回無報錯,再次請求時出現死鎖異常。導致後台和其他相關的後台服務無法對該錶進行任何操作。1 查詢導致死鎖的session和object name 被鎖死的表 select l.session id,o.owner,o.object name from v locked ob...