Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Getting actual number of users using sp_who

Author  Topic 

simondeutsch
Aged Yak Warrior

547 Posts

Posted - 2002-08-15 : 13:52:03
Hiya,
How do I filter the results from sp_who to know how many 'real' users are logged on to SQL Server? Right now, I am the only user logged on, yet I have rows, one for each SPID, of which seven seem to be system processes. I see that the user name for all these is 'sa', but maybe it's possible for a real user to log on as 'sa', too, so I don't know if that's a good criteria.

Sarah Berger MCSD

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2002-08-15 : 14:41:49
This should be close...

select count(*) from master.dbo.sysprocesses where sid <> 0X01

 


Jay White
{0}
Go to Top of Page

simondeutsch
Aged Yak Warrior

547 Posts

Posted - 2002-08-15 : 14:51:20
Thank you very much, Jay. It works well. Am I right in assuming that all system processes get their sid defaulted to 0X01?
As an afterthought, I realized I could also use sp_who2, and use the application name for criteria, since I am primarily concerned about users in a specific application.

Sarah Berger MCSD
Go to Top of Page

MuffinMan
Posting Yak Master

107 Posts

Posted - 2002-08-15 : 17:58:42
Sarah, I wrote the follow SP to show me who's logged in and what they are doing (it shows the first 255 characters of the SQL statment the user issued*). You can easily adapt for your purpose.

*NOTE: the SQL statement shown on any particular records may be incorrect due to the time it takes to run the DBCC command. Use the sp will some caution.

MuffinMan


CREATE procedure sp_ProcessInfo
as
set nocount on

declare @loop int , @MaxRecs int , @stmt varchar(255) , @sql varchar(100)

create table #stmt (EventType varchar(255) , parameters int , Eventinfo varchar(255) )

create table #processes ( Rowid int identity(1,1) primary key clustered not null,
dbid smallint, DatabaseName varchar(15), Program_Name varchar(30), spid smallint, blocked smallint
, last_batch datetime, open_tran smallint, waittime int,
HostName varchar(20), NT_Username varchar(20), Login_Name varchar(20), Status varchar(20), Cmd varchar(20),
LastWaitType varchar(20), WaitResource varchar(20), Physical_IO int,
MemUsage int, uid smallint, cpu int,
SQL_Stmt_255 varchar(255)
)

INSERT #processes (
dbid , DatabaseName , spid , blocked , last_batch ,
open_tran , waittime , LastWaitType , WaitResource , Physical_IO ,
MemUsage , uid , cpu , HostName , Program_Name , NT_Username ,
Login_Name , Status , Cmd
)
Select p.dbid , Substring(d.name,1,15) As DatabaseName , p.spid , p.blocked
, p.last_batch , p.open_tran , p.waittime, Substring(p.LastWaitType,1,20) As LastWaitType
, Substring(p.WaitResource,1,20) As WaitResource , p.Physical_IO , p.MemUsage , p.uid
, p.cpu , Substring(p.hostname,1,20) As HostName , Substring(p.program_name,1,30) As Program_Name
, Substring(p.nt_username,1,20) as NT_Username , Substring(p.loginame,1,20) As Login_Name
, Substring(p.Status,1,20) As Status , Substring(p.Cmd,1,20) as Cmd
from master..sysprocesses as p with (nolock)
inner join master..sysdatabases as d with (nolock)
on p.dbid = d.dbid
--order by d.name , p.Last_Batch , p.spid
order by case when p.blocked > 0 then -1 else 0 end , p.Last_Batch , d.name , p.program_name ,p.spid


select @loop = 0 , @MaxRecs = @@rowcount , @stmt = '' , @sql = ''


while @loop < @MaxRecs
begin
truncate table #stmt

Select @loop = @loop + 1

select @sql = 'dbcc inputbuffer(' + convert(varchar(10), spid) + ')' from #processes where rowid = @loop

--print @sql
insert #stmt
exec (@sql)

Update #processes
set SQL_Stmt_255 = Replace(b.Eventinfo,char(13)+char(10), ' ')
from #processes a, #stmt b
where RowID = @loop
end


select * from #processes
order by rowid

drop table #processes , #stmt
GO




Go to Top of Page

simondeutsch
Aged Yak Warrior

547 Posts

Posted - 2003-03-06 : 12:37:59
Another question: How do I return the User-defined logins from the syslogins table? I don't want the Builtin/Adminstrator, and the Servername/Administrator logins. They have a different sid, but I don't know how to query the sid column.

Sarah Berger MCSD
Go to Top of Page
   

- Advertisement -