How to find active connections, and related information to a SQL Server database
Having the ability to inspect the active connections to SQL Server, who is logged on and what they are doing, can be very important information when troubleshooting database issues.
It can also be required when you want to perform some exclusive work on you database(s) or for other database maintenance activities.
Struxureware Power Monitoring
Power Monitoring Expert
SQL Server 2005, SQL Server 2008 R2, SQL Server 2012, SQL Server 2014, SQL Server 2016
Troubleshooting possible SQL database issues.
*Warning: Irreparable database damage can occur. This procedure should only be performed by users familiar with SQL Server Management Studio. Databases should be backed up prior to performing this procedure.*
There are several ways to find information about the active connections to your SQL Server. Below are some possible methods, each varying the amount and
information returned. This information can also be filtered to return only those processes that are not idle, those belonging to a specific user, or those belonging
to a specific session in order limit the volume to only that information which is of interest.
1) Activity Monitor
- Use the SQL Server Management Studio --> Management --> Activity Monitor
- This gives a list of users, database names, status, command, transactions, application with which they are using the database, CPU, IO and Memory usage, Login time etc.
2) Stored Procedure(s)
- Use the built in stored procedure(s) called sp_who or sp_who2
- sp_who2 is an undocumented procedure which returns more information than sp_who
- Run the command exec sp_who or exec sp_who2
- This gives a list of users, database names, status, command, program with which they are using the database, CPU time, Login time etc.
3) Sql Script (Systemprocesses)
- Use the following script or a variation of it to query the required information from the sys.systemprocesses view of the Master database
- This script can be tailored to obtain the information required (ie. more or less) for the individual purpose
- This script returns the Database, Number of open connections and logged-in user credentials
- This script returns the status, login name and host name for the database you specify.
SELECT DB_NAME(dbid) as DBName, COUNT(dbid) as NumberOfConnections,loginame as LoginName
WHERE dbid > 0
GROUP BY dbid, loginame
SELECT spid, status, loginame, hostname, blocked, db_name(dbid), cmd
WHERE db_name(dbid) = 'databasename'
4) Sql Script (Dynamic Management Views)
- SQL Server 2005 introduced Dynamic Management Views, in this case we can just check if the is_user_process is 1 instead of checking for the value of spid.
- This will work in SQL Server 2005 and 2008.
SELECT count(*) as ConnectionCount
WHERE is_user_process = 1
If you want both system and user information:
SELECT count(*) as ConnectionCount,
CASE when is_user_process =1 then 'UserProcess' else 'System Process' end
GROUP by is_user_process
Also, can look at the sys.dm_exec_connections Dynamic Management Views. This view only shows user connections.
** see attachment containing the SQL script text