I need the number of connections for a specific database

  • What script do I use to find the number of users in a specifc database?

    I found this in another post but I believe it shows ALL connections.

    Select * from master.dbo.sysperfinfo where counter_name = 'User connections'

    Thanks a bunch.

    Lonnie Johnson
    May God Bless You Beyond Your Imagination
    Prodev

  • SELECT SD.[Name], COUNT(1) AS [Users]

    FROM master.dbo.sysdatabases SD INNER JOIN master.dbo.sysprocesses SP

    ON SD.dbid = SP.dbid

    GROUP BY SD.[Name]

  • ...or,

    select db_name(dbid), count(*) from sys.sysprocesses

    group by db_name(dbid)

    David

    @SQLTentmaker

    “He is no fool who gives what he cannot keep to gain that which he cannot lose” - Jim Elliot

  • DavidB (10/3/2008)


    ...or,

    select db_name(dbid), count(*) from sys.sysprocesses

    group by db_name(dbid)

    Nice, no wonder I could not find the sysprocesses table. Tried seperating it to sys.processes.

  • Thanks guys. That did it. I am moving on.

    Hey, would anyone have script to force or clear those users from that database? Thanks again. I actually did this. I am writing an windows program.

    Dim strSQL As String = "SELECT db_name(dbid) as DatabaseName, count(dbid) as NoOfConnections, " _

    & "loginame as LoginName " _

    & "FROM sysprocesses " _

    & "WHERE dbid > 0 and db_name(dbid) = '" & Me.ddlDatabases.Text & "' " _

    & "GROUP BY dbid, loginame"

    Lonnie Johnson
    May God Bless You Beyond Your Imagination
    Prodev

  • I found the script to kill the users. Thanks.

    Lonnie Johnson
    May God Bless You Beyond Your Imagination
    Prodev

  • lcjohnson (10/3/2008)


    I found the script to kill the users. Thanks.

    Use with great caution! If you hit the wrong SPID, you can tank the server or some critical poorly written sproc that can't withstand a kill. If you just want to get all the users out of a server so you can do maintenance, consider setting the database to "Single User" and then back to normal, instead. Still and again, use with great caution. 😉

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

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