How to Get Current Users and Applications Connected to Microsoft SQL Server

ms-sql-server

ms-sql-server

Sometimes you want to view the current users and applications that are connected to Microsoft SQL server. I have developed an SQL script that it just does that.

You can open a query window in SQL Server Management Studio and run the below query.

New Query Window:

NewQuery

NewQuery

SQL SCRIPT:

select
convert(varchar(10), login_time, 101) + ',' 'Login Date,',
convert(varchar(14), login_time, 14) + ',' 'Login Time,',
convert(varchar(20), rtrim(loginame)) + ',' 'Login Name,',
convert(varchar(25), rtrim(nt_domain)) + ',' 'Network Domain,',
convert(varchar(20), rtrim(hostname)) + ','  'Workstation Name,',
convert(varchar(40), rtrim(program_name)) + ',' 'Application Name,',
convert(varchar(10), last_batch, 101) + ',' 'CMD Date,',
convert(varchar(14), last_batch, 14) + ',' 'CMD Time,',
convert(varchar(13), rtrim(ma.status)) + ',' 'Status,',
convert(varchar(13), rtrim(net_library)) + ',' 'Connection Type,',
convert(varchar(15), rtrim(us.name)) 'Group Type'
from master..sysprocesses ma, sysusers us
where ma.uid = us.uid

You can also download the query using the following link.

Download Query Script_DWCurrSQLConnections.

If you want to download a store procedure use the following link:

Download as Store Procedure pS_DWCurrSQLConnections.