Skip to content

Instantly share code, notes, and snippets.

@gwalkey
Created September 13, 2022 20:56
Show Gist options
  • Save gwalkey/763d214febb8bf2601b6d32380d02beb to your computer and use it in GitHub Desktop.
Save gwalkey/763d214febb8bf2601b6d32380d02beb to your computer and use it in GitHub Desktop.
SQL Server - Connections Per Database
--- Simple
SELECT
@@SERVERNAME AS 'Server',
DB_NAME(dbid) as 'Database',
COUNT(dbid) as 'Number Of Open Connections'
FROM
sys.sysprocesses
WHERE
dbid > 0
GROUP BY
dbid
with rollup
order by
2
--- Detailed
SELECT
@@SERVERNAME AS 'Server',
sd.name DBName,
sp.dbid,
loginame [Login],
hostname,
[program_name] 'Program Name',
[spid],
last_batch LastBatch,
blocked BlkBy,
sp.[status],
cmd Command,
cpu CPUTime,
physical_io DiskIO
FROM sysprocesses sp
JOIN sysdatabases sd ON sp.dbid = sd.dbid
where sp.loginame<>'sa'
ORDER BY 2, 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment