--Set the Deadlock Trace
DBCC TRACEON (1222, -1)
DECLARE @RawLogs table (id int IDENTITY (1, 1),logdate datetime, processinfo nvarchar(50), logtext nvarchar(max))
--Reading the error logINSERT INTO @RawLogs
EXEC sp_readerrorlog
DECLARE @results table (id int IDENTITY (1,1), logdate datetime, processinfo nvarchar(50), logtext nvarchar(max))
DECLARE @ids table (id int, processinfo nvarchar(50))
--Filtering Deadlocks
INSERT into @ids
SELECT id, processinfo
FROM @RawLogs
WHERE logtext = 'deadlock-list' ORDER by id
DECLARE @Startid int, @endid int, @processinfo nvarchar(50)SELECT TOP 1 @Startid = id from @ids ORDER by id
--Sorting the deadlocks
WHILE(@@rowcount<>0)
BEGIN
SELECT @processinfo = processinfo from @ids where id = @Startid
SELECT TOP 1 @endid = id from @ids where id > @Startid and processinfo = @processinfo ORDER by id
INSERT into @results (logdate, processinfo, logtext)
SELECT logdate, processinfo, logtext FROM @RawLogs
where id >=@Startid and processinfo = @processinfo andid < @endid order by id
DELETE @ids where id = @Startid
SELECTtop 1 @Startid = id from @ids order by id
END
SELECT logdate, processinfo, logtext FROM @results
DBCC TRACEON (1222, -1)
DECLARE @RawLogs table (id int IDENTITY (1, 1),logdate datetime, processinfo nvarchar(50), logtext nvarchar(max))
--Reading the error logINSERT INTO @RawLogs
EXEC sp_readerrorlog
DECLARE @results table (id int IDENTITY (1,1), logdate datetime, processinfo nvarchar(50), logtext nvarchar(max))
DECLARE @ids table (id int, processinfo nvarchar(50))
--Filtering Deadlocks
INSERT into @ids
SELECT id, processinfo
FROM @RawLogs
WHERE logtext = 'deadlock-list' ORDER by id
DECLARE @Startid int, @endid int, @processinfo nvarchar(50)SELECT TOP 1 @Startid = id from @ids ORDER by id
--Sorting the deadlocks
WHILE(@@rowcount<>0)
BEGIN
SELECT @processinfo = processinfo from @ids where id = @Startid
SELECT TOP 1 @endid = id from @ids where id > @Startid and processinfo = @processinfo ORDER by id
INSERT into @results (logdate, processinfo, logtext)
SELECT logdate, processinfo, logtext FROM @RawLogs
where id >=@Startid and processinfo = @processinfo andid < @endid order by id
DELETE @ids where id = @Startid
SELECTtop 1 @Startid = id from @ids order by id
END
SELECT logdate, processinfo, logtext FROM @results
No comments:
Post a Comment