November 7, 2006 at 1:43 am
Let say I have a system that restricted maximun 5 users login concurrently. I want to check the number of users currently using the system so that I can prompt an error message when the 6th user trying to login. Anyone has the solution ?
Thanks.
November 7, 2006 at 8:31 am
sp_who returns info about users and processes connected to SQL Server. It's in BooksOnLine.
Greg
Greg
November 7, 2006 at 9:03 am
It kind of sounds like he wants a limit for an application, to prevent more than 5 instances, instead of 5 users to the sql database.... am i right?
you'd still use SP_WHO or SP_WHO2 to find out who's connected, but you'd most likely filter on the ProgramName column of that data, and count those users.
You would need to make sure your application uses that in the connection string; ie
oconnection.open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Northwind;Data Source=eg;Application Name=TestApp"
the application would then have to say there are 5 users already connected and terminate.
Lowell
November 7, 2006 at 6:02 pm
If I filter on the ProgramName column, I need to count the number of user using HostName column ? How if 2 different users connected have same HostName ?
November 7, 2006 at 7:34 pm
assuming that one machine can run as many instances as you want, but only 5 users, this might get you started:
in my poor example below, you can see that the machine STORMSQL is using two instances of enterprise manager. If i were to uncomment my filter and ProgramName='MyApplication'
we would end up with only rows that have different machines AND using the correct application;
from that recordset you can count rows and make your application bail out or ask for more licenses or whatever.
since hostname=machinename, you generally have one person using the machine unless someone is using a terminal server or something; so you could do a double restriction: no more than 5 instances on any one machine, and no more than 5 hostnames using the application.
NumInstancesPerMachine | hostname | ProgramName |
2 | STORMSQL | MS SQLEM |
1 | STORMSQL | SQL Query Analyzer |
1 | STORMSQL | SQL Query Analyzer - Object Browser |
create table #tempwho (
SPID int ,
Status varchar(60) ,
Login varchar(60) ,
HostName varchar(60) ,
BlkBy varchar(60) ,
DBName varchar(60) ,
Command varchar(60) ,
CPUTime int ,
DiskIO int ,
LastBatch varchar(60) ,
ProgramName varchar(60) ,
SPID2 int )
insert into #tempwho
exec sp_who2
select count(ProgramName) as NumInstancesPerMachine, hostname,ProgramName from #tempwho
where spid >=50
--and ProgramName='MyApplication'
group by hostname,ProgramName
drop table #tempwho
Lowell
November 7, 2006 at 9:00 pm
Thanks so much. I've solved my problem.
November 7, 2006 at 9:04 pm
hope what i posted was helpful.
Lowell
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply