简体   繁体   English

如何查找谁最后修改了SQL Server中的表?

[英]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. 我试图找到最后修改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. sys.dm_db_index_usage_stats提供有关表的上次修改日期的信息,但我正在寻找上次修改者。 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. 我认为您必须在SQL Server中启用默认情况下未启用的内置审核,或者编写您自己的审核发生情况的方法。

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. 我在触发器中使用了以下脚本来获取last_modified_by。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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