如果用户更改 table HelloWorlds
,那么我想要“他们所做的操作”,他们做的时间,以及原始行的副本插入到HelloWorldsHistory
.
由于列长度,我宁愿避免单独触发插入、更新和删除操作。
我试过这个:
create trigger [HelloWorlds_After_IUD] on [HelloWorlds]
FOR insert, update, delete
as
if @@rowcount = 0
return
if exists (select 1 from inserted) and not exists (select 1 from deleted)
begin
insert into HelloWorldHistory (hwh_action, ..long column list..)
select 'INSERT', helloWorld.id, helloWorld.text ... and more from inserted
end
else
if exists (select 1 from inserted) and exists (select 1 from deleted)
begin
insert into HelloWorldHistory (hwh_action, ..long column list..)
select 'UPDATE', helloWorld.id, helloWorld.text ... and more from deleted
end
else
begin
insert into HelloWorldHistory (hwh_action, ..long column list..)
select 'DELETE', helloWorld.id, helloWorld.text ... and more from deleted
end
end
我从未见过插入出现,但我见过更新。我将尝试 3 个单独的触发器,尽管维护列列表不会很有趣。