October 3, 2008 at 9:13 am
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
October 3, 2008 at 9:46 am
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]
October 3, 2008 at 10:13 am
...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
October 3, 2008 at 10:16 am
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.
October 3, 2008 at 10:24 am
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
October 3, 2008 at 7:45 pm
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
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply