资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

日常SQL数据库死锁跟踪及处理

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

成都创新互联是一家专注于做网站、成都网站制作与策划设计,丹寨网站建设哪家好?成都创新互联做网站,专注于网站建设十载,网设计领域的专业建站公司;建站业务涵盖:丹寨等地区。丹寨做网站价格咨询:13518219792

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  '引起数据库死锁的是: ' + 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语法如下'

        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


本文名称:日常SQL数据库死锁跟踪及处理
链接地址:http://cdkjz.cn/article/gghppc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220