SQL Server进程时间运行和ip地址

时间:2014-09-25 20:25:56

标签: sql-server sql-server-2008 tsql

我想制作一个SELECT或存储过程,以了解运行一个进程的时间和地点(ip地址)。

SELECT 
   conn.session_ID as SPID,
   conn.client_net_address as IPAddress,
   sess.host_name as MachineName,
   sess.program_name as ApplicationName,
   login_name as LoginName
FROM 
   sys.dm_exec_connections conn
INNER JOIN 
   sys.dm_exec_sessions sess ON conn.session_ID = sess.session_ID

我有这个,但只显示了ip地址。

1 个答案:

答案 0 :(得分:1)

听起来你需要添加

sess.last_request_start_time,
sess.last_request_end_time,

http://msdn.microsoft.com/en-GB/library/ms176013.aspx

如果上次请求尚未结束,您可以使用DATEDIFF在日期和ISNULL之间获取秒数,以将当前时间替换为结束时间。

SELECT 
conn.session_ID as SPID,
conn.client_net_address as IPAddress,
sess.host_name as MachineName,
sess.program_name as ApplicationName,
login_name as LoginName,
sess.last_request_start_time,sess.last_request_end_time,
DATEDIFF(second,sess.last_request_start_time,ISNULL(sess.last_request_end_time,getdate())) last_request_seconds
FROM sys.dm_exec_connections conn
inner join sys.dm_exec_sessions sess
on conn.session_ID=sess.session_ID
WHERE conn.session_ID <> @@SPID -- Don't include this query