How to Find Who Is Using SQL Server

Galaxy Glossary

How do I find out who is using SQL Server right now?

Quickly list active sessions, users, and queries running on SQL Server.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

What does “who uses SQL Server” mean?

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.

How do I list active sessions fast?

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.

Which system views give the most detail?

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.

How can I monitor the Orders database only?

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.

Can I join to application tables?

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.

Best practices for tracking user activity

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.

Why How to Find Who Is Using SQL Server is important

How to Find Who Is Using SQL Server Example Usage


-- Which users are currently creating new Orders?
SELECT
    s.login_name,
    r.command,
    r.start_time,
    QUOTENAME(c.database_id) AS db_id,
    SUBSTRING(st.text, r.statement_start_offset/2, 
                      (CASE WHEN r.statement_end_offset = -1 
                            THEN LEN(CONVERT(NVARCHAR(MAX), st.text))*2 
                            ELSE r.statement_end_offset END - r.statement_start_offset)/2) AS sql_text
FROM sys.dm_exec_sessions            AS s
JOIN sys.dm_exec_connections         AS c ON s.session_id = c.session_id
JOIN sys.dm_exec_requests            AS r ON s.session_id = r.session_id
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) AS st
WHERE DB_NAME(c.database_id) = 'Orders'
  AND r.command LIKE '%INSERT%';

How to Find Who Is Using SQL Server Syntax


-- Quick check with built-in proc
EXEC sp_who2;

-- Detailed DMV pattern
SELECT
    s.session_id           AS spid,
    s.login_name           AS login,
    s.host_name            AS workstation,
    c.client_net_address   AS ip,
    DB_NAME(c.database_id) AS database_name,
    r.status,
    r.command,
    r.cpu_time,
    r.blocking_session_id  AS blocker
FROM sys.dm_exec_sessions    AS s
JOIN sys.dm_exec_connections AS c ON s.session_id = c.session_id
LEFT JOIN sys.dm_exec_requests   AS r ON s.session_id = r.session_id
WHERE s.session_id  > 50 -- skip system processes
  AND DB_NAME(c.database_id) = 'Orders';

Common Mistakes

Frequently Asked Questions (FAQs)

Does sp_who2 hurt performance?

No. It is lightweight and reads system metadata only. However, repetitive calls in a tight loop can generate unnecessary noise in logs.

How can I see the exact SQL text?

Join sys.dm_exec_requests to sys.dm_exec_sql_text using CROSS APPLY. This returns the running statement for each session.

Can I get historical user activity?

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.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.