Hello, I have the following which gives me what I need minus the database name. I know how to get it when I am looking for blocking but that is not what I want. I just am looking for the database a session is connected to.
SELECT
Con.session_id AS SessionID
, Cses.host_name AS HostName
, Cses.login_name AS LoginName
, Cses.program_name AS ProgramName
, (-- Query gets text as XML for all queries
SELECT [text] AS [text()]
FROM sys.dm_exec_sql_text(Con.most_recent_sql_handle)
FOR XML PATH(''), TYPE
) AS CodeText
, Con.connect_time AS ConnectTime
, Con.last_read AS LastRead
, 'connection database code' AS DatabaseName
FROM sys.dm_exec_connections Con
INNER JOIN sys.dm_exec_sessions Cses ON Con.session_id = Cses.session_id
ORDER BY LoginName
;
Thank you,
DJJ