May 1, 2007 at 5:18 am
im using sys.dm_exec_sessions to show me all current sessions ( instead of using sp_who2). the problem is that sys.dm_exec_sessions doesnt have the database name or id. where can i map the spid to , to get the db name?
May 1, 2007 at 7:49 am
Hello Smith,
Can you check whether this query gives out the desired results you want?
select
es.session_id, sp.spid, sp.dbid, object_name(dbid)
from
sys.dm_exec_sessions es inner join sys.sysprocesses sp on es.session_id = sp.spid
Thanks
Lucky
May 1, 2007 at 11:24 am
Correction: you should use db_name(dbid), not object_name
June 21, 2012 at 2:43 pm
But the question was, and is how to get it from sys.dm_exec_sessions. You are joining to the sysprocesses which in deprecated by MS SQL Server 2012 to the sys.dm_ ... views
read here http://msdn.microsoft.com/en-us/library/ms179881.aspx
JIM
June 21, 2012 at 2:51 pm
How about this:
select
es.session_id,
er.session_id,
er.dbid,
db_name(er.dbid)
from
sys.dm_exec_sessions es
inner join sys.dm_exec_requests er on es.session_id = er.session_id;
Edit: Corrected a couple of aliases.
June 21, 2012 at 3:40 pm
Nope. sorry but you have sp. in there and no AS for sp. GTry running it yourself
June 21, 2012 at 3:47 pm
james-1023125 (6/21/2012)
Nope. sorry but you have sp. in there and no AS for sp. GTry running it yourself
Yep, I missed changing a couple of sp aliases. Sorry.
June 21, 2012 at 3:49 pm
james-1023125 (6/21/2012)
Nope. sorry but you have sp. in there and no AS for sp. GTry running it yourself
Also, didn't see you suggest any alternatives, so keep your snarkiness to yourself please.
June 21, 2012 at 3:49 pm
This should do it
select
es.session_id,
er.database_id,
db_name(er.database_id) AS DBName
from
sys.dm_exec_sessions es
inner join sys.dm_exec_requests er on es.session_id = er.session_id;
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
June 22, 2012 at 7:46 am
Yep, that works, thanks. Did not mean to offend. The G was a typo
thanks
JIM
January 7, 2013 at 11:42 am
This query (joining sys.dm_exec_sessions to sys.dm_exec_requests) is functionally different from querying sys.sysprocesses.
sys.sysprocesses has the current database context for every session (regardless of whether there are any currently-executing requests).
However, joining to sys.dm_exec_requests means that you will only get results for sessions with currently-executing requests.
You'll get fewer roes using sys.dm_exec_requests, and potentially filter out valid results.
As of now, I think that the dbid data in sys.sysprocesses is uniquely available in that view, and not available in any other 2012 DMV.
September 8, 2015 at 1:07 pm
sys.dm_exec_requests will show only the currently executing processes not the one waiting for connections
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply