2017-06-22 53 views
1

我需要檢測列和表上的重命名操作。 我可以看到在此查詢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中的表或列,我不能在此查詢檢測。還有另一種方法可以做到嗎?

問候。

回答

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/

+0

論哪個表?在哪個事件?整個數據庫上的 –

+0

(我編輯答案) –