Labels

Thursday, 24 November 2016

Script to find who did what in SQL Server ?

Below script is very use full to find out  who has performed what kind of action in SQL Server.

SELECT sys.dm_exec_sessions.session_id
 ,sys.dm_exec_sessions.host_name
 ,sys.dm_exec_sessions.program_name
 ,sys.dm_exec_sessions.client_interface_name
 ,sys.dm_exec_sessions.login_name
 ,sys.dm_exec_sessions.nt_domain
 ,sys.dm_exec_sessions.nt_user_name
 ,sys.dm_exec_connections.client_net_address
 ,sys.dm_exec_connections.local_net_address
 ,sys.dm_exec_connections.connection_id
 ,sys.dm_exec_connections.parent_connection_id
 ,sys.dm_exec_connections.most_recent_sql_handle
 ,(
  SELECT TEXT
  FROM master.sys.dm_exec_sql_text(sys.dm_exec_connections.most_recent_sql_handle)
  ) AS sqlscript
 ,(
  SELECT db_name(dbid)
  FROM master.sys.dm_exec_sql_text(sys.dm_exec_connections.most_recent_sql_handle)
  ) AS databasename
 ,(
  SELECT object_id(objectid)
  FROM master.sys.dm_exec_sql_text(sys.dm_exec_connections.most_recent_sql_handle)
  ) AS objectname
FROM sys.dm_exec_sessions
INNER JOIN sys.dm_exec_connections ON sys.dm_exec_connections.session_id = sys.dm_exec_sessions.session_id


No comments:

Post a Comment