简体   繁体   中英

How to find who last modified the table in SQL server?

I am trying to find the user who last modified particular table in SQL Server. sys.dm_db_index_usage_stats provides information about last modified date for the table but I am looking for last modified by. Is there a way to figure out which login last modified specific table?

I think you'd have to enable the built in auditing in SQL Server, which isn't enabled by default, or, write your own method of auditing what happens.

If this is for something that has already happened, then I think you might be out of luck.

Try this out and see if it helps:

DECLARE @filename VARCHAR(255) 
SELECT @FileName = SUBSTRING(path, 0, LEN(path)-CHARINDEX('\', REVERSE(path))+1) + '\Log.trc'  
FROM sys.traces   
WHERE is_default = 1;  

SELECT gt.HostName, 
       gt.ApplicationName, 
       gt.NTUserName, 
       gt.NTDomainName, 
       gt.LoginName, 
       gt.SPID, 
       gt.EventClass, 
       te.Name AS EventName,
       gt.EventSubClass,      
       gt.TEXTData, 
       gt.StartTime, 
       gt.EndTime, 
       gt.ObjectName, 
       gt.DatabaseName, 
       gt.FileName, 
       gt.IsSystem
FROM [fn_trace_gettable](@filename, DEFAULT) gt 
JOIN sys.trace_events te ON gt.EventClass = te.trace_event_id 
WHERE EventClass in (164) 
ORDER BY StartTime DESC; 

Thank You everyone for looking into my question and providing suggestions. I used below script in my trigger to get the last_modified_by.

   DECLARE @login_name NVARCHAR(200)
   SELECT  @login_name = loginame
   FROM  sys.sysprocesses
   WHERE   spid = @@SPID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM