Wednesday, 4 March 2015

Who is logged on to the SQL Server?

This tiny script will tell you who is logged on to SQL Server, and which Database they are connected to. Simply run at an SQL Command Prompt, or within Management Studio:

SELECT DB_NAME(dbid) as DBName, 
       COUNT(dbid) as NumberOfConnections, 
       loginame as LoginName 
FROM sys.sysprocesses 
WHERE dbid > 0 
GROUP BY dbid, loginame
ORDER BY DBName