码迷,mamicode.com
首页 > 数据库 > 详细

Sql数据库查询当前环境有无死锁

时间:2017-01-03 22:07:09      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:nbsp   nio   where   spi   count   --   lin   create   roc   

 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 
            print @@ERROR

        INSERT  INTO #tmp_lock_who
                ( spid ,
                  bl
                )
                SELECT  0 ,
                        blocked
                FROM    ( SELECT    *
                          FROM      master..sysprocesses
                          WHERE     blocked > 0
                        ) a
                WHERE   NOT EXISTS ( SELECT *
                                     FROM   ( SELECT    *
                                              FROM      master..sysprocesses
                                              WHERE     blocked > 0
                                            ) b
                                     WHERE  a.blocked = spid )
                UNION
                SELECT  spid ,
                        blocked
                FROM    master..sysprocesses
                WHERE   blocked > 0
        IF @@ERROR <> 0 
            print @@ERROR

-- 找到临时表的记录数
        SELECT  @intCountProperties = COUNT(*) ,
                @intCounter = 1
        FROM    #tmp_lock_who

        IF @@ERROR <> 0 
            print @@ERROR

        IF @intCountProperties = 0 
            SELECT  现在没有阻塞和死锁信息 AS message
-- 循环开始
        WHILE @intCounter <= @intCountProperties 
            BEGIN
-- 取第一条记录
                SELECT  @spid = spid ,
                        @bl = bl
                FROM    #tmp_lock_who
                WHERE   id = @intCounter
                BEGIN
                    IF @spid = 0 
                        SELECT  引起数据库慢的是:  + CAST(@bl AS VARCHAR(10))
                                + 进程号,其执行的SQL语法如下
                    ELSE 
                        SELECT  进程号SPID: + CAST(@spid AS VARCHAR(10)) + 
                                + 进程号SPID: + CAST(@bl AS VARCHAR(10))
                                + 阻塞,其当前进程执行的SQL语法如下
                    DBCC INPUTBUFFER (@bl )
                END
-- 循环指针下移
                SET @intCounter = @intCounter + 1
            END

        DROP TABLE #tmp_lock_who
        print 0

 

Sql数据库查询当前环境有无死锁

标签:nbsp   nio   where   spi   count   --   lin   create   roc   

原文地址:http://www.cnblogs.com/x-poior/p/6246469.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!