How to see sessions that are currently established in the database

set nocount on
SELECT @DBName = ‘databaseName’
begin try
drop table #tmpSpwho2
end try
begin catch
end catch
CREATE TABLE #tmpSpwho2 (SPID INT, Status VARCHAR(100), Login VARCHAR(100), HostName VARCHAR(100), Blkby VARCHAR(100), DBName VARCHAR(100), Command VARCHAR(50), CPUTime INT,
DiskIO INT, LastBatch VARCHAR(100), ProgramName VARCHAR(100), SPID2 INT, REQUESTID INT)

INSERT INTO #tmpSpwho2 EXEC sp_who2
select * FROM #tmpSpwho2 WHERE DBName = @DBName


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s