Home > Microsoft .Net Development Tips > Microsoft SQL Server > Problem-solving blocking situations
Win Development Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

MICROSOFT SQL SERVER

Problem-solving blocking situations


Robert Hauck
10.07.2003
Rating: --- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


This procedure is intended to help with problem-solving blocking situations on the server. I developed it to contact users when their client software holds locks that are holding up other users. This works faster than viewing the blocking status through Enterprise Manager. It has been tested on SQL Server 2000.

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO


/*Authored by Bob Hauck
@SendMessage - If set to true message will be sent via net send to client machine
@KillBlock   - If set to true the first offending process will be killed.
@Extension   - A phone number to have the user call.  Sent in net send message
*/

Create         PROCEDURE Blocking_Analysis  @SendMessage bit = 0 ,
      @KillBlock bit = 0,
      @Extension VarChar(12) = '0'
AS

Declare @WorkStation as varchar(8000)
Declare @SPID as smallint
Declare @NetSend as varchar(8000)
Declare @NT_UserName as varchar(8000)

--@SendMessage -- T = send message to user
   -- F = don't send message to user
--@KillBlock  -- T = Kill blocking process
   -- F = dont kill blocking process
--@Extension -- phone extension to have user call
Create table #temp ( spid smallint, 
   TreadID smallint,
   Blocked_by smallint,
   Wait_Type binary(2),
   Wait_Time int,
   Last_Wait_Type nchar(32),
   Wait_Resource nchar(256),
   Database_ID smallint,
   UserID smallint,
   CPU int,
   Physical_IO bigint,
   Memory_Usage int,
   Log_in_Time datetime,
   Last_Batch datetime,
   Execution_Context smallint,
   Open_Transactions smallint,
   Status nchar(30),
   GUID_or_sid binary(86),
   WorkStation nchar(128),
   Application nchar(128),
   WorkStation_PID nchar(8),
   Current_Command nchar(16),
   NT_Domain nchar(128),
   NT_UserName nchar(128),
   net_address nchar(12),
   net_library nchar(12),
   Login_Name nchar(128),
   [context_info] binary(128),
   SQL_Handle binary(20),
   stmt_start int,
   stmt_end int
      )
--Populate table with blocking data from system tables
insert #temp Select * from master.dbo.sysprocesses where blocked <> 0 or spid in (
 Select blocked from master.dbo.sysprocesses where blocked <> 0)
--Retrieve information about the blocking process
Select  @Workstation = Workstation, 
 @SPID = spid ,
 @NT_UserName = nt_username
from #temp where Blocked_by = 0
--List the involved processes and label the blocking status
select Case when Blocked_by = 0 
 then 'Blocking others' 
 else 'Blocked by ' + ltrim(rtrim(@NT_UserName))
 end'Status',
 ltrim(rtrim(NT_UserName))'User', 
 ltrim(rtrim(WorkStation))'WorkStation' 
from #temp order by blocked_by

--Send net send message to client PC to call the supplied extension 
If @SendMessage = 1
Begin

 Set @NetSend = 'Net Send ' + ltrim(rtrim(convert(varchar,@Workstation))) + 
  ' "Please call extension ' + @Extension + ' ASAP"'
 print @NetSend
 EXEC master..xp_cmdshell @NetSend
End
-- Kill the offending process if requested  default is not to kill
If @KillBlock = 1 and isnull(@SPID,-1) != -1
Begin
 EXEC ('KILL ' + @SPID)
 If @@Error = 0
 Begin
  Print 'Process ID ' + convert(varchar,@SPID) + ' has been terminated on machine ' + @Workstation
 End
 Else
 Begin
  Print 'An Error was encountered while trying to stop process ' 
   + convert(varchar,@SPID) + ' Error code ' + convert(varchar,@@Error)
 End
End

drop table #temp

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

For More Information

  • Feedback: E-mail the editor with your thoughts about this tip.
  • More tips: Hundreds of free SQL Server tips and scripts.
  • Tip contest: Have a SQL Server tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize -- submit your tip today!
  • Ask the Experts: Our SQL, database design, Oracle, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.
  • Forums: Ask your technical SQL Server questions--or help out your peers by answering them--in our active forums.
  • Best Web Links: SQL Server tips, tutorials, and scripts from around the Web.

Rate this Tip
To rate tips, you must be a member of SearchWinDevelopment.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
SQL Server and .NET development
Microsoft releases new CTP of Oslo SDK
Perpetuum unveils database synchronizer for .NET 2.0
The CTE, the hierarchical query and SQL Server 2005
SQL Server 2005 recursive functions and the CTE
SQL Examiner Suite synchronizes data schema, sets
Book excerpt: ADO.NET and SQL Server 2005
DataDirect database drivers now support MySQL
Top .NET tips of 2007 (so far)
Addressing common SQL Server questions
Book Excerpt: The .NET Framework and SQL Server 2005

Microsoft SQL Server
Using the Visual Studio 2005 DataSet Designer to build a data access layer
The CTE, the hierarchical query and SQL Server 2005
SQL Server 2005 recursive functions and the CTE
Choose the right .NET data provider, optimize application performance
The fallacy of the data layer -- or, a new architectural model for software designs
Book excerpt: ADO.NET and SQL Server 2005
Addressing common SQL Server questions
Book Excerpt: The .NET Framework and SQL Server 2005
WinForms development using SQL Server 2005 and Visual Basic 2005
Configuring ASP.NET 2.0 apps to SQL Server 2005 databases

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Database Programming Solutions - .NET XML, Visual Studio LINQ, ORM .NET
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts