Monday, September 9, 2024

How do I find the SPID (Session Process ID) for a specific database in SQL Server?

 SELECT 

    es.session_id AS SPID, 

    es.login_name, 

    es.status, 

    es.host_name, 

    es.program_name, 

    es.database_id, 

    DB_NAME(es.database_id) AS database_name,

    ec.client_net_address,

    er.wait_type, 

    er.wait_time, 

    er.blocking_session_id

FROM sys.dm_exec_sessions es

LEFT JOIN sys.dm_exec_connections ec ON es.session_id = ec.session_id

LEFT JOIN sys.dm_exec_requests er ON es.session_id = er.session_id

WHERE es.database_id = DB_ID('YourDatabaseName');

------------------------------------------------------------------------------------

kill SSID