如何检测表和列的重命名操作?

时间:2017-06-22 14:01:10

标签: sql-server ssms table-rename

我需要检测列和表的重命名操作。 我可以在此查询中看到alterdropcreate操作:

DECLARE   @filename nvarchar(1000);

SELECT   @filename = cast(value as nvarchar(1000))
FROM   ::fn_trace_getinfo(default)
WHERE   traceid = 1 and   property = 2;

SELECT   *
FROM   fn_trace_gettable(@filename, default) AS ftg 
INNER   JOIN sys.trace_events AS te ON ftg.EventClass = te.trace_event_id  
left outer join sys.all_objects o on o.name = ftg.ObjectName
order by EventSequence DESC;

但是当有人在MSSQL Management Studio上重命名表或列时,我无法检测到此查询。还有其他办法吗?

问候。

1 个答案:

答案 0 :(得分:0)

您可以创建数据库触发器。 可用事件列表:[sys].[events](有ALTER_COLUMN事件)

来自msdn的例子:

CREATE TRIGGER safety   
ON DATABASE   
FOR DROP_SYNONYM  
AS   
   RAISERROR ('You must disable Trigger "safety" to drop synonyms!',10, 1)  
   ROLLBACK  
GO  
DROP TRIGGER safety  
ON DATABASE;  
GO  

另一个例子:https://www.mssqltips.com/sqlservertip/2085/sql-server-ddl-triggers-to-track-all-database-changes/