新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
使用存儲過程檢查引起死鎖的SQL語句及進程
使用存儲過程,可以檢測是哪些SQL語句及進程造成死鎖,下面就將為您介紹該存儲過程,供您參考,希望對您學習SQL有所幫助。

假如發(fā)生了死鎖,我們怎么去檢測具體發(fā)生死鎖的是哪條SQL語句或存儲過程?此時我們可以使用以下存儲過程來檢測,就可以查出引起死鎖的進程和SQL語句。
| 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ù) select @intCountProperties = Count(*),@intCounter = 1 from #tmp_lock_who IF @@ERROR<>0 RETURN @@ERROR if @intCountProperties=0 select ’現(xiàn)在沒有阻塞和死鎖信息’ as message – 循環(huán)開始 while @intCounter <= @intCountProperties begin – 取***條記錄 select @spid = spid,@bl = bl from #tmp_lock_who where Id = @intCounter begin if @spid =0 select ’引起數(shù)據(jù)庫死鎖的是: ’+ CAST(@bl AS VARCHAR(10)) + ’進程號,其執(zhí)行的SQL語法如下’ else select ’進程號SPID:’+ CAST(@spid AS VARCHAR(10))+ ’被’ + ’進程號SPID:’+ CAST(@bl AS VARCHAR(10)) +’阻塞,其當 前進程執(zhí)行的SQL語法如下’DBCC INPUTBUFFER |
【編輯推薦】
SQL語句中CASE WHEN的使用實例
教您不帶參數(shù)的SQL語句執(zhí)行的方法
巧用GO將多次重復執(zhí)行SQL語句
SQL中INSERT語句的使用技巧
SQL語句中output的用法
網(wǎng)站標題:使用存儲過程檢查引起死鎖的SQL語句及進程
本文鏈接:http://www.dlmjj.cn/article/djjohgo.html


咨詢
建站咨詢
