sys.dm_exec_sessions joining with
sys.sysprocesses gives the details of active connections in server. It helps us to find out the performance issues, which database are in use while the server is slow and so on
SELECT @@Servername AS Server ,
DB_NAME(dbid) AS DatabaseName ,
COUNT(dbid) AS Connections ,
Login_name AS LoginName ,
MIN(es.Login_Time) AS Login_Time ,
MIN(COALESCE(last_request_end_time, last_request_start_time))
AS Last_Batch
FROM sys.dm_exec_sessions es
inner join sys.sysprocesses sp on es.session_id = sp.spid
WHERE dbid > 0
AND DB_NAME(dbid) NOT IN ( 'master', 'msdb' )
GROUP BY dbid ,
login_name
ORDER BY DatabaseName;