Microsoft SQL Server - Send e-mails with block notifications
Sometimes we need to send an e-mail when our SQL Server identify some blocking process.
The script below will help you. Just create a job, change the information of your mail profile, recipients, subject and body.
The script below will help you. Just create a job, change the information of your mail profile, recipients, subject and body.
SET NOCOUNT ON;
DECLARE @blockingProcess INT
SELECT @blockingProcess = COUNT (*)
FROM sys.dm_tran_locks L
JOIN sys.partitions P ON P.hobt_id = L.resource_associated_entity_id
JOIN sys.objects O ON O.object_id = P.object_id
JOIN sys.dm_exec_sessions ES ON ES.session_id = L.request_session_id
JOIN sys.dm_tran_session_transactions TST ON ES.session_id = TST.session_id
JOIN sys.dm_tran_active_transactions AT ON TST.transaction_id = AT.transaction_id
JOIN sys.dm_exec_connections CN ON CN.session_id = ES.session_id
CROSS APPLY
sys.dm_exec_sql_text(CN.most_recent_sql_handle) AS ST
WHERE resource_database_id
= db_id()
if @blockingProcess != 0
begin
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'myprofile',
@recipients = 'myemail@email.com',
@subject = 'My subject',
@body = 'Blocking Process found.'
end
SET NOCOUNT off;
Comments
Post a Comment