使用存儲(chǔ)過(guò)程檢查引起死鎖的SQL語(yǔ)句及進(jìn)程
作者:佚名
如果發(fā)生了死鎖,如何才能查出引起死鎖的進(jìn)程和SQL語(yǔ)句?我們可以建立存儲(chǔ)過(guò)程來(lái)檢測(cè),下面就將為您介紹該存儲(chǔ)過(guò)程,供您參考,
使用存儲(chǔ)過(guò)程,可以檢測(cè)是哪些SQL語(yǔ)句及進(jìn)程造成死鎖,下面就將為您介紹該存儲(chǔ)過(guò)程,供您參考,希望對(duì)您學(xué)習(xí)SQL有所幫助。
假如發(fā)生了死鎖,我們?cè)趺慈z測(cè)具體發(fā)生死鎖的是哪條SQL語(yǔ)句或存儲(chǔ)過(guò)程?此時(shí)我們可以使用以下存儲(chǔ)過(guò)程來(lái)檢測(cè),就可以查出引起死鎖的進(jìn)程和SQL語(yǔ)句。
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 – 找到臨時(shí)表的記錄數(shù) select @intCountProperties = Count(*),@intCounter = 1 from #tmp_lock_who IF @@ERROR<>0 RETURN @@ERROR if @intCountProperties=0 select ’現(xiàn)在沒(méi)有阻塞和死鎖信息’ as message – 循環(huán)開(kāi)始 while @intCounter <= @intCountProperties begin – 取***條記錄 select @spid = spid,@bl = bl from #tmp_lock_who where Id = @intCounter begin if @spid =0 select ’引起數(shù)據(jù)庫(kù)死鎖的是: ’+ CAST(@bl AS VARCHAR(10)) + ’進(jìn)程號(hào),其執(zhí)行的SQL語(yǔ)法如下’ else select ’進(jìn)程號(hào)SPID:’+ CAST(@spid AS VARCHAR(10))+ ’被’ + ’進(jìn)程號(hào)SPID:’+ CAST(@bl AS VARCHAR(10)) +’阻塞,其當(dāng) 前進(jìn)程執(zhí)行的SQL語(yǔ)法如下’DBCC INPUTBUFFER |
【編輯推薦】
責(zé)任編輯:段燃
來(lái)源:
互聯(lián)網(wǎng)