August 31, 2010 at 2:26 pm
Hi Folks
I am sure this has been asked a thousand times, but what sql can i run or system tables can I access to determine who or what is accessing a particular database.
Thanks
jim
August 31, 2010 at 2:29 pm
sp_who (less detail) or sp_who2 (more detail) are the most popular. They are pretty much what you see in the Activity Monitor, which is the GUI version.
With DMVs a good one is: sys.dm_exec_sessions (check example A on the link)
http://msdn.microsoft.com/en-us/library/ms176013.aspx
Shawn Melton
Twitter: @wsmelton
Blog: wsmelton.github.com
Github: wsmelton
August 31, 2010 at 2:35 pm
Hi Shawn
thanks very much
i will give them all ashot
Jim
August 31, 2010 at 2:48 pm
Hi Shawn
i do not see the dbname in the table (sys.dm_exec_sessions) as i do in sp_who or sp_who2.
and i do not see where i can use sys.dm_exec_sessions to get the dbname
any ideas
Thanks
Jim
August 31, 2010 at 2:55 pm
Well that is true, sys.dm_exec_sessions is for seeing what a particular user/login is doing. Forgot about that.
Try using sys.sysprocesses (http://msdn.microsoft.com/en-us/library/ms179881%28SQL.90%29.aspx). This one has the dbid column to filter by the databse ID.
Shawn Melton
Twitter: @wsmelton
Blog: wsmelton.github.com
Github: wsmelton
August 31, 2010 at 3:21 pm
Thanks Shawn
i was able to use two tables to get what i wanted for now:
select
a.dbid,
CAST (b.name as varchar(90)) as database_name,
CAST (a.loginame as varchar(36)) as loginame,
CAST (a.hostname as varchar(18)) as hostname,
cpu,
a.login_time,
CAST (a.status as varchar(18)) as status,
CAST (a.program_name as varchar(50)) as program_name,
from
sys.sysprocesses a,
sys.sysdatabases b
where
a.dbid=b.dbid
order by
b.name;
go
Jim
August 31, 2010 at 3:27 pm
sorry
had an extra comma after program_name:
select
a.dbid,
CAST (b.name as varchar(90)) as database_name,
CAST (a.loginame as varchar(36)) as loginame,
CAST (a.hostname as varchar(18)) as hostname,
cpu,
a.login_time,
CAST (a.status as varchar(18)) as status,
CAST (a.program_name as varchar(50)) as program_name
from
sys.sysprocesses a,
sys.sysdatabases b
where
a.dbid=b.dbid
order by
b.name;
go
August 31, 2010 at 5:08 pm
JC-3113 (8/31/2010)
sorryhad an extra comma after program_name:
select
a.dbid,
CAST (b.name as varchar(90)) as database_name,
CAST (a.loginame as varchar(36)) as loginame,
CAST (a.hostname as varchar(18)) as hostname,
cpu,
a.login_time,
CAST (a.status as varchar(18)) as status,
CAST (a.program_name as varchar(50)) as program_name
from
sys.sysprocesses a,
sys.sysdatabases b
where
a.dbid=b.dbid
order by
b.name;
go
You can remove the join to sysdatabases by using the db_name (dbid).
August 31, 2010 at 6:23 pm
pankushmehta (8/31/2010)
JC-3113 (8/31/2010)
sorryhad an extra comma after program_name:
select
a.dbid,
CAST (b.name as varchar(90)) as database_name,
CAST (a.loginame as varchar(36)) as loginame,
CAST (a.hostname as varchar(18)) as hostname,
cpu,
a.login_time,
CAST (a.status as varchar(18)) as status,
CAST (a.program_name as varchar(50)) as program_name
from
sys.sysprocesses a,
sys.sysdatabases b
where
a.dbid=b.dbid
order by
b.name;
go
You can remove the join to sysdatabases by using the db_name (dbid).
Which will have to query the sysdatabases behind the scenes. You might as well as keep it.
HOWEVER: sys.sysprocesses and sys.sysdatabases are both backwards-compatible views. Unless they have some information that you can't get anywhere else, you should be using the new sys.databases, etc. views.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
August 31, 2010 at 6:35 pm
There are also a couple more options.
there is a script called sp_whoisactive by Adam machanic here
I also wrote an article with a script that may be of use here[/url]
I also have other scripts on my blog (in my sig).
To get historical information, there are triggers and auditing that would be useful.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
September 1, 2010 at 10:12 am
Thanks pankushmehta
September 1, 2010 at 10:13 am
Hi Jason
unfortunately i work at an AFB and they block alot of sites & blogs so i cannot get to them
i will have to look at them from home
Jim
September 1, 2010 at 10:15 am
Thanks Waynes
i am new to sql server sql; coming from oracle
will have to look at these tables you mention
Jim
September 1, 2010 at 10:22 am
JC-3113 (9/1/2010)
Hi Jasonunfortunately i work at an AFB and they block alot of sites & blogs so i cannot get to them
i will have to look at them from home
Jim
It would be worth your time. That's unfortunate that many blogs are blocked - I have the same issue here.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
September 2, 2010 at 5:38 am
SELECT db_name(dbid) as DatabaseName, count(dbid) as NoOfConnections,
loginame as LoginName FROM sys.sysprocesses
WHERE dbid > 0 GROUP BY dbid, loginame
Viewing 15 posts - 1 through 15 (of 16 total)
You must be logged in to reply to this topic. Login to reply