pr_DeadlockList.txt 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. CREATE PROCEDURE pr_Deadlock_List
  2. /******************************************************************************
  3. ** File:
  4. ** Name: pr_Deadlock_List
  5. ** Desc: Lists recent deadlocks.
  6. */
  7. AS
  8. BEGIN
  9. DECLARE @Deadlocks TABLE
  10. (
  11. RowID INT IDENTITY(1,1),
  12. LocalTime DATETIME,
  13. DeadlockReport NVARCHAR(4000),
  14. Proc1 NVARCHAR(300) NULL,
  15. Line1 INT NULL,
  16. Proc2 NVARCHAR(300) NULL,
  17. Line2 INT NULL,
  18. PRIMARY KEY(RowID)
  19. );
  20. DECLARE @Loop INT;
  21. DECLARE @Count INT;
  22. DECLARE @Proc1 NVARCHAR(300);
  23. DECLARE @Proc2 NVARCHAR(300);
  24. DECLARE @Line1 INT;
  25. DECLARE @Line2 INT;
  26. DECLARE @Search1 INT;
  27. DECLARE @Search2 INT;
  28. DECLARE @Search3 INT;
  29. DECLARE @Search4 INT;
  30. DECLARE @DeadlockReport NVARCHAR(4000);
  31. SET NOCOUNT ON;
  32. WITH
  33. --get full path to current system_health trace file
  34. CurrentSystemHealthTraceFile AS (
  35. SELECT CAST(target_data AS xml).value('(/EventFileTarget/File/@name)[1]', 'varchar(255)') AS FileName
  36. FROM sys.dm_xe_session_targets
  37. WHERE
  38. target_name = 'event_file'
  39. AND CAST(target_data AS xml).value('(/EventFileTarget/File/@name)[1]', 'varchar(255)') LIKE '%\system[_]health%'
  40. )
  41. --get trace folder name and add base name of system_health trace file with wildcard
  42. , BaseSystemHealthFileName AS (
  43. SELECT
  44. REVERSE(SUBSTRING(REVERSE(FileName), CHARINDEX(N'\', REVERSE(FileName)), 255)) + N'system_health*.xel' AS FileNamePattern
  45. FROM CurrentSystemHealthTraceFile
  46. )
  47. --get xml_deadlock_report events from all system_health trace files
  48. , DeadLockReports AS (
  49. SELECT CAST(event_data AS xml) AS event_data
  50. FROM BaseSystemHealthFileName
  51. CROSS APPLY sys.fn_xe_file_target_read_file ( FileNamePattern, NULL, NULL, NULL) AS xed
  52. WHERE xed.object_name like 'xml_deadlock_report'
  53. )
  54. --display 10 most recent deadlocks
  55. INSERT INTO @Deadlocks (LocalTime,DeadlockReport)
  56. SELECT DATEADD(hour, DATEDIFF(hour, SYSUTCDATETIME(), SYSDATETIME()), event_data.value('(/event/@timestamp)[1]', 'datetime2')) AS LocalTime
  57. , CONVERT(NVARCHAR(4000),event_data) AS DeadlockReport
  58. FROM DeadLockReports
  59. ORDER BY LocalTime ASC;
  60. SELECT @Count = COUNT(*) FROM @Deadlocks;
  61. SET @Loop = 1;
  62. WHILE @Loop <= @Count
  63. BEGIN
  64. SELECT @DeadlockReport = DeadlockReport
  65. FROM @Deadlocks
  66. WHERE RowID = @Loop;
  67. SET @Proc1 = NULL;
  68. SET @Proc2 = NULL;
  69. SET @Line1 = NULL;
  70. SET @Line2 = NULL;
  71. SET @Search1 = CHARINDEX('<frame procname=',@DeadlockReport);
  72. IF @Search1 > 0
  73. BEGIN
  74. SET @Search2 = CHARINDEX('"',@DeadlockReport,@search1+17);
  75. IF @Search2 > 0
  76. BEGIN
  77. SET @Proc1 = SUBSTRING(@DeadlockReport,@search1+17,@Search2-@Search1-17);
  78. SET @Search2 = CHARINDEX('<process ',@DeadlockReport,@Search1);
  79. IF @Search2 > 0
  80. BEGIN
  81. SET @Search2 = CHARINDEX('<frame procname=',@DeadlockReport,@Search2);
  82. SET @Search3 = CHARINDEX(' line="',@DeadlockReport,@Search1);
  83. IF @Search3 > 0 AND @Search3 < @Search2
  84. BEGIN
  85. SET @Search4 = CHARINDEX('"',@DeadlockReport,@Search3+7);
  86. IF @Search4 > 0
  87. BEGIN
  88. SET @Line1 = CONVERT(INT,SUBSTRING(@DeadlockReport,@search3+7,@Search4-@Search3-7));
  89. END;
  90. END;
  91. SET @Search3 = CHARINDEX('"',@DeadlockReport,@Search2+17);
  92. IF @Search3 > 0
  93. BEGIN
  94. SET @Proc2 = SUBSTRING(@DeadlockReport,@Search2+17,@Search3-@Search2-17);
  95. SET @Search3 = CHARINDEX(' line="',@DeadlockReport,@Search2);
  96. IF @Search3 > 0
  97. BEGIN
  98. SET @Search4 = CHARINDEX('"',@DeadlockReport,@Search3+7);
  99. IF @Search4 > 0
  100. BEGIN
  101. SET @Line2 = CONVERT(INT,SUBSTRING(@DeadlockReport,@search3+7,@Search4-@Search3-7));
  102. END;
  103. END;
  104. END;
  105. END;
  106. END;
  107. END;
  108. UPDATE @Deadlocks
  109. SET Proc1 = @Proc1,
  110. Proc2 = @Proc2,
  111. Line1 = @Line1,
  112. Line2 = @Line2
  113. WHERE RowID = @Loop;
  114. SET @Loop = @Loop + 1;
  115. END;
  116. SELECT LocalTime,Proc1,Line1,Proc2,Line2,DeadlockReport
  117. FROM @Deadlocks;
  118. END
  119. GO