Quick Search for:  in language:    
SQL,wrote,bottlenecked,working,with,some,badl
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
SQL Stats

 Code: 45,316. lines
 Jobs: 126. postings

 How to support the site

 
Sponsored by:

 
You are in:
 
Login





Latest Code Ticker for SQL.
sp_db_paging_pr imary_key
By Marcus LeBlanc on 1/11


sp_db_paging
By Marcus LeBlanc on 1/11


Beginners pack
By hardik shah on 1/11


Inline Views
By Thivya Prabakaran on 1/6


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

sp_CatchProcesses

Print
Email
 
VB icon
Submitted on: 8/6/2002 4:44:46 AM
By: Keith Henry 
Level: Advanced
User Rating: By 3 Users
Compatibility:SQL Server 7.0

Users have accessed this code 4026 times.
 
(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 bottle
--     necked SQL box working with some badly w
--     ritten 3rd party code that my company wa
--     s unable to change. It quickly gives a b
--     reakdown of all waiting/blocked processe
--     s and what they are waiting for
-- By: Keith Henry
--
-- Returns:Table containing blocked/wait
--     ing info. Most from sysprocesses/self-ex
--     planatory except: HoldingDescription - D
--     escription of whatever is blocking, Wait
--     ResourceString - Description of waitreso
--     urce code, [SQL Statement] - the first 2
--     55 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 a
--     nywhere else remove this prefix.
--
--This code is copyrighted and has-- limited warranties.Please see http://
--     www.Planet-Source-Code.com/vb/scripts/Sh
--     owCode.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 notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
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 AM:Rasputin
Hey Keith,
Excellent stuff!  Thanks 
for sharing this.
5 planets to 
you!
- Ras
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/22/2003 2:27:15 PM:
For SQL 2000.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | SQL Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.