如何检测表和列的重命名操作?
How can I detect renaming operations on tables and columns?
我需要检测对列和 table 的重命名操作。
我可以在此查询中看到 alter
、drop
和 create
操作:
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 上重命名 table 或列时,我无法检测到此查询。还有其他方法吗?
此致。
您可以创建数据库触发器。
可用事件列表:[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/
我需要检测对列和 table 的重命名操作。
我可以在此查询中看到 alter
、drop
和 create
操作:
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 上重命名 table 或列时,我无法检测到此查询。还有其他方法吗?
此致。
您可以创建数据库触发器。
可用事件列表:[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/