Command to know no of users connected to particular database?

  • Hi,

    Coule you please anybody let me know the command to know the no of users connected to the particular database

    i ran this sp_who2 but it is not extactly giving the results

    Koti

  • Hmmm how about...

    select count(*) from sys.dm_exec_requests where db_name(database_id) = 'MyDatabaseName'

  • Hi Koti,

    Can you try this

    select * from sys.sysprocesses where spid >50 and dbid= dbid

  • Hi,

    The below query will hold good only to know the active users connected to the database ( running state). However if the spid is in sleeping state..that is not captured by the below query.

    select count(*) from sys.dm_exec_requests where db_name(database_id) = 'MyDatabaseName'

    Though the spid is not active, it holds the connection and the resources like memory will be still occupied by that connection.

    So I would prefer querying sys.sysprocesses

    select * from sys.sysprocesses where dbid= 'databaseid'

    As per your requirement you can choose anyone..

    Regards,

    Rajini

  • Yes, that query returns a more comprehensive result.

    I was trying to find a way to use a DMV rather than querying a system table.

  • Thank you Rajini

    I will try this and let you know.

    koti

  • Hi

    i will try this and let you know

    koti

  • Here is the query you are looking for

    SELECT DB_NAME(dbid) as 'Database Name',

    COUNT(dbid) as 'Total Connections'

    FROM master.dbo.sysprocesses WITH (nolock)

    WHERE dbid > 0

    GROUP BY dbid

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply