SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,494 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello, am aiming to set up a SQL 2016 Agent job that runs say every 15 mins and would like to email me if the Sessions count is greater > 10 where each Sessions have statuses IN ('RUNNING', 'RUNNABLE', 'SUSPENDED') and provide at least the following info columns:
Can anyone provide me SQL script or point me in direction to do this please?
Thanks in advance.
Hi,
Please check this:
select
SDES.session_id,SDES.login_name,
SDES.login_time,SDES.host_name,
SDES.status,SDES.last_request_end_time,
DEST.TEXT
from sys.dm_exec_sessions SDES
inner join sys.dm_exec_connections SDEC on SDES.session_id=SDEC.session_id
CROSS APPLY sys.[dm_exec_sql_text](SDEC.[most_recent_sql_handle]) AS DEST
where SDES.status in ('RUNNING', 'RUNNABLE', 'SUSPENDED')
if (select count(*) from (above query) as t) >10
begin
--send email
end