How can I detect renaming operations on tables and columns?

52 Views Asked by At

I need to detect renaming operation on columns and tables. I can see alter, drop and create operation in this query:

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;

But when someone renames a table or column on MSSQL Management Studio, I cannot detect on this query. Is there another way to do it?

Regards.

1

There are 1 best solutions below

1
Mikhail Lobanov On BEST ANSWER

You can create a database trigger. List of available events: [sys].[events] (there are ALTER_COLUMN event)

Example from 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  

another example: https://www.mssqltips.com/sqlservertip/2085/sql-server-ddl-triggers-to-track-all-database-changes/