Skip to content

Instantly share code, notes, and snippets.

@blinds52
Forked from magnetikonline/README.md
Created April 15, 2017 18:50
Show Gist options
  • Save blinds52/241415d5d9616b55fc973812db099069 to your computer and use it in GitHub Desktop.
Save blinds52/241415d5d9616b55fc973812db099069 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