溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

日常SQL數(shù)據(jù)庫死鎖跟蹤及處理

發(fā)布時(shí)間:2020-06-28 12:57:04 來源:網(wǎng)絡(luò) 閱讀:254 作者:kanshan 欄目:關(guān)系型數(shù)據(jù)庫

DECLARE @spid INT
DECLARE @blk INT
DECLARE @count INT
DECLARE @index INT
DECLARE @lock TINYINT

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

--select @count,@index

--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  '引起數(shù)據(jù)庫死鎖的是: ' + Cast(@spid AS VARCHAR(10)) + '進(jìn)程號(hào),其執(zhí)行的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)) + '進(jìn)程號(hào),其執(zhí)行的SQL語法如下' 
        ELSE
          SELECT '進(jìn)程號(hào)SPID:' + Cast(@spid AS VARCHAR(10)) + '被' + '進(jìn)程號(hào)SPID:' + Cast(@blk AS VARCHAR(10)) + '阻塞,其當(dāng)前進(jìn)程執(zhí)行的SQL語法如下'

        PRINT ( LTRIM(@spid) + ''+ LTRIM(@blk));
        if(@spid <> 0)
        BEGIN
           DBCC inputbuffer(@spid)   --
         END

        DBCC inputbuffer(@blk)   --引起阻塞語句

        SET @index=@index + 1
    END

END

DROP TABLE #temp_who_lock

--return 0
--KILL 54

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI