views:

161

answers:

3

how to know how many users are connected to a particular database

+1  A: 

You can use the stored procedure sp_who2

Robin Day
A: 

For a simply query, you can use this on SQL 2000 to SQL 2008 (there is no 1:1 replacement for sysprocesses in SQL 2005)

SELECT
    COUNT(*)
FROM
    MASTER..sysprocesses
WHERE
    dbid = DB_ID('MyDBName')
gbn
I believe this query will get you a wrong count if parallel queries are being executed. You're also not removing the system processes from the result.
LeoPasta
A: 
SELECT COUNT(DISTINCT spid)
FROM master.dbo.sysprocesses
WHERE spid >= 50
AND dbid = DB_ID('MyDBName')
LeoPasta