Software >> Services >> DB >> SQLServer >>  How to use sqlcmd.exe to obtain the number of open db connections to user databases

dbsession.bat ------------- sqlcmd -S <server-ip> -i dbsession.sql >> dbsession.out dsession.sql ------------ SET NOCOUNT ON; SELECT "Open DB sessions at : ", SYSDATETIME(); -- /////////////////////////////////////////////////////////////////////////// -- print '' -- print '---------------------------------------------------------------------------' -- print 'Active session count' -- print '---------------------------------------------------------------------------' -- /////////////////////////////////////////////////////////////////////////// DECLARE @temp TABLE(spid int , ecid int, status varchar(50), loginname varchar(50), hostname varchar(50), blk varchar(50), dbname varchar(50), cmd varchar(50), request_id int) INSERT INTO @temp EXEC sp_who SELECT dbname,loginname,COUNT(*) FROM @temp WHERE loginname <> 'sa' and dbname not in ( 'master','msdb') group by dbname,loginname