Skip to content

Instantly share code, notes, and snippets.

@magnetikonline
Last active April 15, 2017 18:50
Show Gist options
  • Save magnetikonline/2115da9c6fe2d43c4edcf72c01ba9c71 to your computer and use it in GitHub Desktop.
Save magnetikonline/2115da9c6fe2d43c4edcf72c01ba9c71 to your computer and use it in GitHub Desktop.
List active connections to Microsoft SQL Server database.

List active connections to Microsoft SQL Server database

Via the master.dbo.sysprocesses system table.

Query example

SELECT [hostname],COUNT(*)
FROM master.dbo.sysprocesses
WHERE
	([dbid] = db_id('DATABASE_NAME')) AND
	([spid] > 50)
GROUP BY [hostname]
ORDER BY 2 DESC
  • Where spid is the session ID, greater than 50 are user processes (what we care about).

  • A listing of database IDs can be returned via:

     SELECT [name],[dbid]
     FROM master.dbo.sysdatabases
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment