Skip to content

Instantly share code, notes, and snippets.

@Otterpohl
Created May 13, 2022 14:04
Show Gist options
  • Save Otterpohl/573b745eb80d5a09f184c74f24380e99 to your computer and use it in GitHub Desktop.
Save Otterpohl/573b745eb80d5a09f184c74f24380e99 to your computer and use it in GitHub Desktop.
Gets number of connections per database
DECLARE @temp_sp_who2 TABLE
(
SPID INT NULL,
Status VARCHAR(1000) NULL,
Login sysname NULL,
HostName sysname NULL,
BlkBy sysname NULL,
DBName sysname NULL,
Command VARCHAR(1000) NULL,
CPUTime INT NULL,
DiskIO BIGINT NULL, -- int
LastBatch VARCHAR(1000) NULL,
ProgramName VARCHAR(1000) NULL,
SPID2 INT NULL,
RequestId INT NULL --comment out for SQL 2000 databases
);
INSERT INTO @temp_sp_who2
EXEC sys.sp_who2;
SELECT DBName,
Login,
COUNT(1) AS connections
FROM @temp_sp_who2
WHERE SPID > 50
AND SPID <> @@SPID
GROUP BY DBName,
Login
ORDER BY DBName,
COUNT(1) DESC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment