Rose
Rose

Reputation: 73

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?

Upvotes: 2

Views: 31409

Answers (3)

Rose
Rose

Reputation: 73

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

Upvotes: 0

FutbolFan
FutbolFan

Reputation: 13733

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; 

Upvotes: 3

Stuart Grassie
Stuart Grassie

Reputation: 3073

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.

Upvotes: 2

Related Questions