home


Display connection and blocking info
Author Nigel Rivett

Do you often need to find out what is being processed on your server?
How long each process has been running, what the command was, what is blocking, how much i/o it is causing?

This stored procedure will display the last command executed and other attributes for all spids or those with a given status.
The default status is 'runnable' as that is the usually requirement.
By calling the procedure with 'all' all spid are displayed.
By calling the proceduer with a spid just that spid is displayed.
By calling the proceduer with a 'blk' all blocked and blocking spids are displayed.

To keep a record or what is happenning on your server output the result of this procedure to a table and schedule the call.

Note: Sometimes the status displayed is not that requested due to the delay between getting the list of spids and the delay.


sp_nrinfo2 will give an output similar to sp_who2 active but also giving blocking spids and their command.


if exists (select * from sysobjects where id = object_id(N'[dbo].[sp_nrinfo]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_nrinfo]
GO

Create procedure sp_nrinfo
@status varchar(20) = 'active' ,
@type   varchar(10) = 'info'
as
/*
Author Nigel Rivett

exec sp_nrinfo			-- all spids with active status
exec sp_nrinfo 'sleeping'	-- all spids whith status sleeping
exec sp_nrinfo 'background'	-- all spids whith status background
exec sp_nrinfo 'sleeping'	-- all spids whith status sleeping
exec sp_nrinfo 'all'		-- all spids
exec sp_nrinfo 'blk'		-- all blocked or blocking spids
exec sp_nrinfo '74'		-- an individual spid - also gives subthreads
select * from master..sysprocesses where spid = 56
*/
set transaction isolation level read uncommitted
set nocount on
-- Get blocking
declare @blkspid varchar(20)

select top 1 @blkspid = convert(varchar(20),spid) from master..sysprocesses 
where spid in (select blocked from master..sysprocesses)
order by blocked

if @blkspid is not null
begin
	select 'blocking spid = ' + @blkspid
	exec ('dbcc inputbuffer (' + @blkspid + ')')
	
	exec ('sp_who2 ' + @blkspid) 
	
	select spid, blocked from master..sysprocesses 
	where spid in (select blocked from master..sysprocesses)
	order by blocked
end





declare	@cmd varchar(1000)

declare @buf varchar(1000) ,
	@id int ,
	@spid int ,
	@maxSpid int
	create table #spid (spid int, command varchar(1000) null)
	create table #temp (x varchar(100), y int, s varchar(1000), id int identity (1,1))
	create table #spids (spid int)
	if isnumeric(@status) = 1
	begin
		insert #spids select @status
	end
	else if @status = 'blk'
	begin
		insert	#spids 
		select spid from master..sysprocesses where blocked <> 0
		union
		select blocked from master..sysprocesses where blocked <> 0
	end
	else if @status = 'active'
		insert	#spids 
		select 	distinct spid 
		from 	master..sysprocesses 
		where 	blocked <> 0
		or	upper(cmd)    not in 	(
				                     'AWAITING COMMAND'
				                    ,'MIRROR HANDLER'
				                    ,'LAZY WRITER'
				                    ,'CHECKPOINT SLEEP'
				                    ,'RA MANAGER'
						)
		or lower(status) <> 'sleeping'
	else
	begin
		insert	#spids select spid from master..sysprocesses where (status = @status or @status = 'all') and ecid = 0
	end
	select 	@spid = 0 ,
		@maxSpid = max(spid)
	from	#spids
	
	if @type = 'info'
	begin
		while @spid < @maxSpid
		begin
			select	@spid = min(spid) from #spids where spid > @spid
			
			select @cmd = 'dbcc inputbuffer (' + convert(varchar(10),@spid) + ')'
			
			delete #temp
			
			insert #temp
			exec (@cmd)
			
			select 	@id = 0 ,
				@buf = ''
			select @buf = @buf + replace(replace(s,char(10),'|'),char(13),'|')
	
			from #temp
	
			insert 	#spid
			select	@spid, @buf
		end
	end
	else
	begin
		insert	#spid select spid, '' from #spids
	end
	
	select 	spid 		= convert(varchar(4),#spid.spid) ,
		status 		= left(s.status,12) ,
		loginame 	= left(s.loginame, 25) ,
		hostname	= left (s.hostname, 10) ,
		BlkBy		= case when s.blocked <> 0 then convert(varchar(3),s.blocked) else '   ' end ,
		DBName		= left(db_name(dbid), 10) ,
		command		= left(s.cmd, 30) ,
		CPUTime		= s.cpu ,
		DiskIO		= s.physical_io ,
		LastBatch	= convert(varchar(23),s.last_batch,121) ,
		ProgramName	= left(s.program_name, 30) ,
		spid		= convert(varchar(4),#spid.spid) ,
		buffercmd	= #spid.command
	from	#spid ,
		master..sysprocesses s
	where	s.spid = #spid.spid
	and 	(ecid = 0 or isnumeric(@status) = 1)
	order by convert(int,s.spid) desc

	drop table #spid
	drop table #temp

go
grant execute on sp_nrinfo to public
go

if exists (select * from sysobjects where id = object_id(N'[dbo].[sp_nrinfo2]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_nrinfo2]
GO

Create procedure sp_nrinfo2
as
	exec sp_nrinfo 'active', 'noinfo'
go
grant execute on sp_nrinfo2 to public
go



home