Blocking Alert Configuration Script
Blocking Alert Configuration Script
GO
SET QUOTED_IDENTIFIER ON
GO
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------------------------
CREATE proc [dbo].[BlockingMonitor]
(
@waittime bigint=1000, -- Period of time (in second) for search blocking processes
@Recipients varchar(2000), -- Recipient(s) of this email (; separated in case of
multiple recipients).
@IsDBMailEnabled bit=1,
@MailProfile varchar(100) -- Mail profile name which exists on the target database
server
)
as
begin
-- EMail Variables
@TableHTML varchar(MAX), -- HTML
@MailSubject varchar(100),
@Subject varchar (100) -- Subject line
--Set @waittime = 1 -- In seconds the amount of time we want a task to wait prior
to being eligible to be returned.
Set @waittime = @waittime * 1000 -- Convert to miliseconds
/* Gather all of our information where the wait time exceeds the parameter value we
supplied */
Insert Into #Blocking
(WaitInSeconds, BlockingSessionId, DatabaseName, BlockingUser, BlockingLocation,
BlockingSQL,
BlockedSessionId, BlockedUser, BlockedLocation, BlockedSQL, [Blocked Individual
Query], wait_type,programname)
Select
Waits.wait_duration_ms / 1000 as WaitInSeconds,
Blocking.session_id as BlockingSessionId,
DB_NAME(Blocked.database_id) as DatabaseName,
Sess.login_name as BlockingUser,
Sess.host_name as BlockingLocation,
BlockingSQL.text as BlockingSQL,
Blocked.session_id as BlockedSessionId,
BlockedSess.login_name as BlockedUser,
BlockedSess.host_name as BlockedLocation,
BlockedSQL.text as BlockedSQL,
SUBSTRING (BlockedSQL.text, -- String
(BlockedReq.statement_start_offset/2) + 1, -- Starting point
((CASE -- Length
WHEN BlockedReq.statement_end_offset = -1 THEN LEN(CONVERT(NVARCHAR(MAX),
BlockedSQL.text)) * 2
ELSE BlockedReq.statement_end_offset
END - BlockedReq.statement_start_offset)/2) + 1) as [Blocked Individual Query],
Waits.wait_type,Sess.program_name
From
sys.dm_exec_connections Blocking
Join sys.dm_exec_requests Blocked ON (Blocking.session_id =
Blocked.blocking_session_id)
Join sys.dm_exec_sessions Sess ON (Blocking.session_id = sess.session_id)
Left Outer Join sys.dm_tran_session_transactions st ON (Blocking.session_id =
st.session_id)
Left Outer Join sys.dm_exec_requests er ON (st.session_id = er.session_id)
Join sys.dm_os_waiting_tasks Waits ON (Blocked.session_id = Waits.session_id)
Join sys.dm_exec_requests BlockedReq ON (Waits.session_id = BlockedReq.session_id)
Join sys.dm_exec_sessions BlockedSess ON (Waits.session_id =
BlockedSess.session_id)
Cross Apply sys.dm_exec_sql_text(Blocking.most_recent_sql_handle) AS BlockingSQL
Cross Apply sys.dm_exec_sql_text(Blocked.sql_handle) AS BlockedSQL
Where
Waits.wait_duration_ms / 1000 > 30 --Mentioned the time in seconds
Order By
WaitInSeconds Desc;
/* If loaded any records in the previous step, proceed to generate the HTML and
send an e-mail */
If Exists (Select 1 From #Blocking)
Begin
/* In this section we are going to replace set the height property for the row, and
for the even rows, change the background color so it stands out
Going to add a border the individual cells
In case there are longer entries in the blocked, blocking and blocked individual
SQL columns we are going to insert a line break after each comma, otherwise, in
Outlook,
the HTML table will become malformed and become (almost) unreadable
Finally, we will assemble the head, body and tail into a single variable
*/
Set @Body = Replace(@Body, '<tr><td>odd</td>', '<tr style="height:20px;">');
Set @Body = Replace(@Body, '<tr><td>even</td>', '<tr style="background-
color:#D8EBFF; height:20px;">') ;