Skip to content

Instantly share code, notes, and snippets.

@wbhinton
Created December 3, 2019 16:26
Show Gist options
  • Save wbhinton/c4bae89dae8c09a040c8f3baa645759c to your computer and use it in GitHub Desktop.
Save wbhinton/c4bae89dae8c09a040c8f3baa645759c to your computer and use it in GitHub Desktop.
Get all active users accessing a SQL DB
-- Similar information can be derived from sp_who
SELECT @@Servername AS Server ,
DB_NAME(database_id) AS DatabaseName ,
COUNT(database_id) AS Connections ,
Login_name AS LoginName ,
MIN(Login_Time) AS Login_Time ,
MIN(COALESCE(last_request_end_time, last_request_start_time))
AS Last_Batch
FROM sys.dm_exec_sessions
WHERE database_id > 0
AND DB_NAME(database_id) NOT IN ( 'master', 'msdb' )
GROUP BY database_id ,
login_name
ORDER BY DatabaseName;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment