Thursday 4 September 2014

SP to find the Lead Blocker


With help of the below SP we can find the Lead blocker details. it will be useful to resolve the blocking issue quickly.

----Text 
----------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

CREATE PROCEDURE [dbo].[sp_leadblocker] as 
begin 
set nocount on 

IF EXISTS (SELECT * FROM master..sysprocesses p1 JOIN master..sysprocesses p2 ON (p1.spid=p2.blocked)) BEGIN 
DECLARE @spid int 

SELECT @spid=p1.spid -- Get the _last_ prime offender 
FROM master..sysprocesses p1 JOIN master..sysprocesses p2 ON (p1.spid=p2.blocked) 
WHERE p1.blocked=0 

SELECT p1.spid, 
p1.status, 
loginame=LEFT(p1.loginame,20), 
hostname=substring(p1.hostname,1,20), 
blk=CONVERT(char(3),p1.blocked), 
db=LEFT(db_name(p1.dbid),10), 
p1.cmd, 
p1.waittype 
FROM master..sysprocesses p1 
JOIN master..sysprocesses p2 ON (p1.spid=p2.blocked) 
WHERE p1.blocked=0 
select @spid -- Return the last lead blocker 
END ELSE BEGIN 
PRINT 'No processes are currently blocking others.' 
select GetDate() 
END 
end

------ Note : we got this code from the web, Please test it and then use on Production servers. ------

No comments:

Post a Comment