One of my friend was asking to get him information from SQL Server to troubleshoot some performance issues.
My initial understanding, to collect data from the Performance counters, however, I realized that my friend is looking for information at grouping HostName, ProgramName etc.
Hence, come up with a small script as below to capture the details every 1 minute(customized as per the request).
If exists(Select 1 From sys.tables where name='Capture_DBConnection')
Drop table Capture_DBConnection
Create Table Capture_DBConnection (CapturedTime datetime,SPID Varchar(MAX), [Status] varchar(50),Login nvarchar(100),HostNAme varchar(5000),BlkBy Varchar(MAX), DBNAME sysname null,Command varchar(MAX),CPUTIME Varchar(MAX), DISKIO Varchar(MAX),
LastBatch Varchar(MAX), ProgramName Varchar(MAX),SPID1 Varchar(MAX), REQUESTID Varchar(MAX))
While(1=1)
Begin
Create Table #Capture_DBConnection (SPID Varchar(MAX), [Status] varchar(50),Login nvarchar(100),HostNAme varchar(5000),BlkBy Varchar(MAX), DBNAME sysname null,Command varchar(MAX),CPUTIME Varchar(MAX), DISKIO Varchar(MAX),
LastBatch Varchar(MAX), ProgramName Varchar(MAX),SPID1 Varchar(MAX), REQUESTID Varchar(MAX))
Insert into #Capture_DBConnection Exec Sp_who2
Insert into Capture_DBConnection
Select getdate(),* from #Capture_DBConnection
Drop table #Capture_DBConnection
Waitfor delay '00:01:00'
END
Usage:
Here are few examples how to use the data collected.
-- Row data
Select * From Capture_DBConnection
-- To get count of connection for specified group
Select CapturedTime, HostName, ProgramName, Count(CapturedTime)
From Capture_DBConnection
Group by CapturedTime, HostName, ProgramName
Hope, this script will help you if you come across such situation.