Created
December 3, 2019 16:26
-
-
Save wbhinton/c4bae89dae8c09a040c8f3baa645759c to your computer and use it in GitHub Desktop.
Get all active users accessing a SQL DB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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