我表的所有更新都希望被记录下来。因此,我决定进行搜索,并遇到了创建数据库触发器的问题。我遵循了一个教程,并且我能够成功地将所有ALTERS记录到表中,但是没有记录任何更新。遵循了本教程:https://jackworthen.com/2018/03/19/creating-a-log-table-to-track-changes-to-database-objects-in-sql-server/
查询:
CREATE TRIGGER [backup_objects]
ON DATABASE
FOR CREATE_PROCEDURE,
ALTER_PROCEDURE,
DROP_PROCEDURE,
CREATE_TABLE,
ALTER_TABLE,
DROP_TABLE,
CREATE_FUNCTION,
ALTER_FUNCTION,
DROP_FUNCTION,
CREATE_VIEW,
ALTER_VIEW,
DROP_VIEW
AS
SET NOCOUNT ON
DECLARE @data XML
SET @data = EVENTDATA()
INSERT INTO [dbo].UserChangelog(databasename, eventtype,
objectname, objecttype, sqlcommand, loginname)
VALUES(
@data.value('(/EVENT_INSTANCE/DatabaseName)[1]', 'varchar(256)'),
@data.value('(/EVENT_INSTANCE/EventType)[1]', 'varchar(50)'),
@data.value('(/EVENT_INSTANCE/ObjectName)[1]', 'varchar(256)'),
@data.value('(/EVENT_INSTANCE/ObjectType)[1]', 'varchar(25)'),
@data.value('(/EVENT_INSTANCE/TSQLCommand)[1]', 'varchar(max)'),
@data.value('(/EVENT_INSTANCE/LoginName)[1]', 'varchar(256)')
)
GO
我以为触发器丢失了“ UPDATE_TABLE”,但不幸的是甚至不存在。
任何人都知道我可能要添加到触发器中的内容吗?从技术上讲,我不需要将重点放在整个数据库上,但这只是一个奖励。
答案 0 :(得分:0)
尝试...
CREATE TRIGGER [backup_objects]
ON on_your_table AFTER INSERT, DELETE, UPDATE AS ....