This code will help you in finding the lead blocker in a sql server.
SQL Code :
declare @a int
declare @b varchar(50)
set nocount ON
print 'Current Server Time: '+cast(getdate() as varchar(23))
select top 1 @a=Blocked from master..sysprocesses
where blocked<>0
order by waittime desc
if (@a is null)
begin
print '*** Blocked Spids WaitTime Information ***'+char(10)+'There is no blocking at this time . . .'
end
else
begin
print '*** Blocked Spids WaitTime Information ***'+char(10)
select top 10 SPID,KPID,Blocked as Blocker,cast((waittime/3600000)as char(10)) as Hours,
cast((waittime/60000) as char(10)) as Minutes,LastWaitType from master..sysprocesses
where blocked<>0
order by waittime desc
select @b=loginame from master..sysprocesses
where spid=@a
print 'SPID '+cast(@a as varchar(5))+' is the Lead Blocker and the details are:'+char(10)+'Loginame: '+@b
select @b=hostname from master..sysprocesses
where spid=@a
print 'Hostname: '+@b+char(10)+'DBCC InputBuffer Output:'
dbcc inputbuffer(@a)
end
set nocount OFF
>>> Number blocking SPIDS can be increased from 10 to any number by changing the number in the "select top 10 SPID,KPID..." code mentioned above.
>>> This gives the lead blocker detail with the input buffer.
>>> Extremely useful when there are numerous blocking and you are unable to find the lead blocker.
>>> The lead blocker is chose with respect to waittime
>>> Hope this helps you!!!
>>> Post your comments and questions in the comment box.
SQL Code :
declare @a int
declare @b varchar(50)
set nocount ON
print 'Current Server Time: '+cast(getdate() as varchar(23))
select top 1 @a=Blocked from master..sysprocesses
where blocked<>0
order by waittime desc
if (@a is null)
begin
print '*** Blocked Spids WaitTime Information ***'+char(10)+'There is no blocking at this time . . .'
end
else
begin
print '*** Blocked Spids WaitTime Information ***'+char(10)
select top 10 SPID,KPID,Blocked as Blocker,cast((waittime/3600000)as char(10)) as Hours,
cast((waittime/60000) as char(10)) as Minutes,LastWaitType from master..sysprocesses
where blocked<>0
order by waittime desc
select @b=loginame from master..sysprocesses
where spid=@a
print 'SPID '+cast(@a as varchar(5))+' is the Lead Blocker and the details are:'+char(10)+'Loginame: '+@b
select @b=hostname from master..sysprocesses
where spid=@a
print 'Hostname: '+@b+char(10)+'DBCC InputBuffer Output:'
dbcc inputbuffer(@a)
end
set nocount OFF
>>> Number blocking SPIDS can be increased from 10 to any number by changing the number in the "select top 10 SPID,KPID..." code mentioned above.
>>> This gives the lead blocker detail with the input buffer.
>>> Extremely useful when there are numerous blocking and you are unable to find the lead blocker.
>>> The lead blocker is chose with respect to waittime
>>> Hope this helps you!!!
>>> Post your comments and questions in the comment box.