Quickly list active sessions, users, and queries running on SQL Server.
Identifying “who uses SQL Server” means listing every active session—login name, host, database, and current statement—to audit access, resolve blocking, and tune performance.
Run sp_who2
or query dynamic management views (DMVs) such as sys.dm_exec_sessions
, sys.dm_exec_connections
, and sys.dm_exec_requests
. These return each session’s user, status, CPU, and I/O usage within milliseconds.
sys.dm_exec_sessions
exposes login, program, and last request times. sys.dm_exec_connections
adds client network address and protocol. sys.dm_exec_requests
shows the exact SQL, wait type, and blocking SPID.
Filter the DMV output: WHERE DB_NAME(c.database_id) = 'Orders'
. You will see only sessions currently connected to the Orders database, useful when troubleshooting slow order inserts.
Yes. Use DB_NAME(c.database_id)
to link sessions back to specific databases that hold business tables like Customers
or Orders
. This helps correlate spikes in activity with query volume against those tables.
Exclude system SPIDs (<50), grant VIEW SERVER STATE
instead of sysadmin
for DMV access, and persist snapshots to a log table every minute to build historical usage trends.
No. It is lightweight and reads system metadata only. However, repetitive calls in a tight loop can generate unnecessary noise in logs.
Join sys.dm_exec_requests
to sys.dm_exec_sql_text
using CROSS APPLY
. This returns the running statement for each session.
Yes. Save DMV snapshots to a table (e.g., every 1 minute via Agent job) and query that log for trends such as peak login times or blocking patterns.