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 > 0ide
--if @@error<>0 return @@error
SELECT @count = Count(*),
@index = 1
FROM #temp_who_lockcode
--select @count,@index進程
--if @@error<>0 return @@error
IF @count = 0
BEGIN
SELECT '沒有阻塞和死鎖信息'
--return 0
ENDinput
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=1it
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
ENDast
IF @lock = 0
BEGIN
SET @index=1class
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語法以下' 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 54select