Important alert: (current site time 7/15/2013 7:57:05 PM EDT)
 

VB icon

sp_CatchProcesses

Email
Submitted on: 8/6/2002 4:44:46 AM
By: Keith Henry 
Level: Advanced
User Rating: By 5 Users
Compatibility: SQL Server 7.0
Views: 14925
(About the author)
 
     I wrote this for a bottlenecked SQL box working with some badly written 3rd party code that my company was unable to change. It quickly gives a breakdown of all waiting/blocked processes and what they are waiting for
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
--**************************************
-- Name: sp_CatchProcesses
-- Description:I wrote this for a bottlenecked SQL box working with some badly written 3rd party code that my company was unable to change. It quickly gives a breakdown of all waiting/blocked processes and what they are waiting for
-- By: Keith Henry
--
-- Returns:Table containing blocked/waiting info. Most from sysprocesses/self-explanatory except: HoldingDescription - Description of whatever is blocking, WaitResourceString - Description of waitresource code, [SQL Statement] - the first 255 chars of the statement the process is trying to run.
--
-- Assumes:This has a sp_ prefix for use in the master database. If you use it anywhere else remove this prefix.
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=520&lngWId=5--for details.--**************************************

create proc sp_CatchProcesses
as
	begin
	set nocount on 
	
	create table #tempInputBuff (EventType varchar(255),Parameters int,EventInfo varchar(255))
	declare	@TempSysprocesses TABLE (
		HoldingDescription varchar(255) null,
		spid smallint NULL ,
		kpid smallint NULL ,
		blocked smallint NULL ,
		waittype binary (2) NULL ,
		waittime int NULL ,
		lastwaittype nchar (32) NULL ,
		waitresource nchar (256) NULL ,
		dbid smallint NULL ,
		uid smallint NULL ,
		status nchar (30) NULL ,
		sid binary (86) NULL ,
		hostname nchar (128) NULL ,
		program_name nchar (128) NULL ,
		hostprocess nchar (8) NULL ,
		cmd nchar (16) NULL ,
		SQLStmt varchar(255) null
	)
	
	declare @tempSpid int
	
	begin tran
	delete
	from @TempSysprocesses 
	insert @TempSysprocesses (HoldingDescription,hostname,program_name,cmd,spid,kpid,waittime,lastwaittype,waitresource,status,dbid)
	select	(
			(	case
					when s.waittime > 0 then (
						'Waiting for ' + 
						(	case rtrim(s.waitresource) 
								when '' then rtrim(s.LastWaitType)
								else	isnull(rtrim(s.waitresource),'unknown') + 
									', Held by' + isnull (
									(	select 	top 1
											' Host: ' + isnull(rtrim(msw.hostname),'') + 
											', Prog: ' + isnull(rtrim(msw.program_name),'') +
											(case when msw.waittime > 0 then ', Wait for: ' + (case rtrim(msw.waitresource) when '' then rtrim(msw.LastWaitType) else isnull(rtrim(msw.waitresource),'Non Found') end) else '' end) +
											(case when msw.blocked > 0 then ', Block by: ' + cast(msw.blocked as varchar) else '' end)
										from	master..sysprocesses as msw with (nolock)
										where	cast(msw.spid as varchar) = s.waitresource
									),' ' + rtrim(s.LastWaitType))
							end
						)
					) + ' ' 
					else ''
				end
			) +
			(	case
					when s.blocked <> 0 then
						'blocked by ID ' + 
						cast(s.blocked as varchar) + 
						', blocked by detail (' + isnull (
						(	select 	top 1
								' Host: ' + isnull(rtrim(msb.hostname),'') + 
								', Prog: ' + isnull(rtrim(msb.program_name),'') +
								(case when msb.waittime > 0 then ', Wait for: ' + (case rtrim(msb.waitresource) when '' then rtrim(msb.LastWaitType) else isnull(rtrim(msb.waitresource),'Non Found') end) else '' end) +
								(case when msb.blocked > 0 then ', Block by: ' + cast(msb.blocked as varchar) else '' end)
							from	master..sysprocesses as msb with (nolock)
							where	msb.spid = s.blocked
						),'') + ') '
					else ''
				end
			) + 
			(	case
					when s.spid in (	select	distinct msb2.blocked
									from	master..sysprocesses as msb2 with (nolock)	) then
						'Blocking'
					else ''
				end
			)
		) as HoldingDescription, s.hostname,s.program_name,s.cmd,s.spid,s.kpid,s.waittime,s.lastwaittype,s.waitresource,s.status,s.dbid
	from	master..sysprocesses as s with (nolock)
	where	(	s.spid in (	select	distinct ms.blocked
					from	master..sysprocesses as ms with (nolock)	) 
		)
		or
		s.blocked <> 0 
		or s.waittime > 0
	if @@rowcount > 0 
	begin
		declare tc cursor for (select spid from @TempSysprocesses where kpid <> 0)
		OPEN tc
			FETCH NEXT FROM tc INTO @tempSpid
			WHILE (@@FETCH_STATUS = 0)
			BEGIN
				delete from #tempInputBuff
				insert #tempInputBuff EXEC ('DBCC INPUTBUFFER (' + @tempSpid + ') WITH NO_INFOMSGS')
				
				update 	@TempSysprocesses
				set	SQLStmt = (select top 1 EventInfo from #tempInputBuff)
				where	spid = @tempSpid
				FETCH NEXT FROM tc INTO @tempSpid
			END
		CLOSE tc
		DEALLOCATE tc
		print 'Locked/Wait at ' + convert(char(8),getdate(),108)
		select	convert(char(8),getdate(),108) as [Time],s.spid,s.kpid,s.cmd,s.HoldingDescription,
			s.hostname, s.program_name,s.waittime,s.lastwaittype,
			cast(rtrim(s.waitresource) as char(32)) as waitresource, s.status, 
			(	select	top 1 d.[name] 
				from	master..sysdatabases d 
				where	d.dbid = s.dbid
			) as [Database],
			(select	'Waiting for ' + (
					case left(s.waitresource,3)
						when 'TAB' then 'Table'
						when 'PAG' then 'Page'
						when 'KEY' then 'Index'
						when 'RID' then 'Row'
						else 'resource'
					end) + ' in ' + db.[Name]
			from	master..sysdatabases as db with (nolock)
			where	db.dbid = (	case 
							when s.waitresource like '%:%' then
								case 	
									when left(s.waitresource,charindex(':',s.waitresource)-1) like '[0-9]%' then left(s.waitresource,charindex(':',s.waitresource)-1)
									else ltrim(substring(s.waitresource,charindex(':',s.waitresource)+1,abs(charindex(':',s.waitresource,charindex(':',s.waitresource)+1) - (charindex(':',s.waitresource)+1))))
								end
							else 0
						end)
			) as WaitResourceString, replace(replace(replace(SQLStmt, char(10) + char(13), ' '),char(10),' '),char(13),' ') as [SQL Statement]
		from 	@TempSysprocesses s
		order by s.spid
	end
 	ELSE
 		print 'No Locks or waits at ' + convert(varchar,getdate(),108)
	commit tran
	drop table #tempInputBuff
end
GO


Other 9 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

8/9/2002 10:43:57 AMRasputin

Hey Keith,

Excellent stuff! Thanks for sharing this.

5 planets to you!

- Ras
(If this comment was disrespectful, please report it.)

 
3/22/2003 2:27:15 PM

For SQL 2000.
(If this comment was disrespectful, please report it.)

 
5/30/2007 3:44:54 AM

If you change waittime to be a bigint (instead of an int) on the declaration of the table @TempSysprocesses, then it will work in both SQL 2000 and SQL 2005

(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.