November 16, 2016 at 2:13 am
Hello,
I am not acutally a beginner, but a simple task like filtering users seems to be a daunting task using the SP_WHO package.
To list sessions of the user SA we would run:
EXEC sp_who @loginame = 'sa'
go
So how do I list all users who are not 'sa' ?
I tried somthing pure SQL understands a human like me:
EXEC sp_who @loginame != 'sa'
go
but no, I tried Goole as usual, but not a single example. Please help?
Thank you
Richard
November 16, 2016 at 2:20 am
I don't think you can do it with sp_who out of the box. What you can do is look at the definition for sp_who and create a new stored procedure (sp_who3?) that has a parameter that excludes the specified login name.
John
November 16, 2016 at 2:21 am
Don't use sp_who (it dates back to pre-SQL 2000 and has not, afaik, been updated for the new DMVs and new information available)
Build your own query from the DMVs (sys.dm_exec_sessions and sys.dm_exec_requests would be the most likely to contain the info you need) and then you can filter as you like.
Or, if you don't want to write your own query from scratch, look up Glenn Berry's diagnostic scripts and use them as a template.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
November 16, 2016 at 2:23 am
CREATE TABLE #temp_sp_who2
(
SPID INT,
Status VARCHAR(1000) NULL,
Login SYSNAME NULL,
HostName SYSNAME NULL,
BlkBy SYSNAME NULL,
DBName SYSNAME NULL,
Command VARCHAR(1000) NULL,
CPUTime INT NULL,
DiskIO INT NULL,
LastBatch VARCHAR(1000) NULL,
ProgramName VARCHAR(1000) NULL,
SPID2 INT
, rEQUESTID INT NULL --comment out for SQL 2000 databases
)
INSERT INTO #temp_sp_who2
EXEC sp_who2
SELECT * FROM #temp_sp_who2
WHERE LOGIN <> 'sa'
DROP TABLE
#temp_sp_who2
You could do something like that too.
How to post a question to get the most help http://www.sqlservercentral.com/articles/Best+Practices/61537
November 16, 2016 at 2:37 am
Thank you all for such a quick response. That looks like MS making life easier for us. I'll mark all answers as "answers", although the CREATE MY OWN TABLE seems like a funny solution, because in order to check what's going on every 1 minute it means DELETE/INSERT multiple times. However, I may go for it as more robust and simple. 😀
November 16, 2016 at 3:27 am
Ha, I decided to put the sp_who into an SQL script and run this in CMD:
sqlcmd -i who.sql | find /v "sa" | find /V "SQLSERVERAGENT" | find /v "Administrator" | find /V "NT SERVICE"
At least I got all my regular users listed.
November 16, 2016 at 9:33 am
Why on Earth do you need to run such a thing once per minute?
--Jeff Moden
Change is inevitable... Change for the better is not.
November 16, 2016 at 9:39 am
When running sp_who2 or DMV queries from SSMS, rather than filtering on 'SA', perhaps excluding your own SPID is what you really want. It's entirely possible that another DBA (or even another user), is logged in as 'SA'.
WHERE SPID <> @@SPID
"Do not seek to follow in the footsteps of the wise. Instead, seek what they sought." - Matsuo Basho
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply