SQL如何发现死锁是由那个SQL语句造成。

解决方案 »

  1.   


    详见:http://www.bitscn.com/pdb/mssql/201010/191346.html
    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
    -- 找到临时表的记录数
    select @intCountProperties = Count(*),@intCounter = 1
    from #tmp_lock_who
    IF @@ERROR<>0 RETURN @@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 
      

  2.   

    在 SQL Server Profiler 里跟踪 Deadlock graph 事件类.
      

  3.   

    maco_wang
    脚本有问题啊!qianjin036a
    怎么跟踪Deadlock graph 事件类?能给个学习的例子吗?
      

  4.   

    参考一下这个吧:
    http://blog.csdn.net/you_tube/archive/2009/07/18/4132850.aspx
      

  5.   

    sql server profiler跟踪一下,就知道是那sql死锁。
      

  6.   

    http://blog.csdn.net/fredrickhu/archive/2009/12/24/5071970.aspx
      

  7.   

    http://blog.csdn.net/fredrickhu/archive/2009/12/24/5071970.aspx
      

  8.   

     习惯自己写 sql 查询死锁
     select * from sys.dm_tran_locks where type = 'OBJECT'
     链接
     select * from sys.dm_exec_requests
     select * from sys.dm_exec_connects 等等查看 执行sql
     
     profile 查Deadlock graph这个 如果用可视化配置来做是没什么难度的