0

I'd like to get active user session count before rebooting instance, exclude any background sessions.

Just thinking to use following query but I'm not sure how to exclude select session for tempdb by SQL Server.

SELECT COUNT(*) FROM sys.dm_exec_requests WHERE session_id in (select session_id from sys.dm_exec_sessions where status = 'running')

Any advice would be appreciated again.

Sachiko
  • 808
  • 1
  • 12
  • 31

2 Answers2

0

If you need only active user sessions and exclude sessions for tempdb

Try this

select count(*) FROM sys.dm_exec_requests 
where 
    database_id <> DB_ID('tempdb') 
    and 
    session_id in 
        (select session_id from sys.dm_exec_sessions where is_user_process = 1 and status='running')
Dmitry Kolchev
  • 2,116
  • 14
  • 16
0

Many thanks for your prompt reply, Dmitry:) I could achive it as follows;

set StrSQL="SET NoCount ON;SELECT COUNT(*) FROM sys.dm_exec_requests WHERE database_id <> DB_ID('tempdb') and session_id in (select session_id from sys.dm_exec_sessions where is_user_process = 1 and status='running' and session_id <> @@spid)"
for /F %%A in ('sqlcmd -Q%StrSQL% %CONN_PARAM%') do set USER_SESSION_CNT=%%A
echo Current User Session Cnt:%USER_SESSION_CNT%

if %USER_SESSION_CNT% gtr 0 goto WARNING_USER_SESSION

Then, Skip the reboot task. Thank you so much for all of your support.

Sachiko
  • 808
  • 1
  • 12
  • 31