How to write trigger for deleted item?
I created two triggers already:
create trigger [insert_history] on users
for 开发者_StackOverflowinsert
as
insert audit(action_type, table_name, object_name, time)
select 'Inserted', 'users', username, getdate()
from inserted
go
create trigger [update_history] on users
for update
as
insert audit(action_type, table_name, object_name, time)
select 'Updated', 'users', username, getdate()
from deleted
Inserted retrieves value from inserated. Update retrieves value from deleted.
What about delete statement?
That would use the deleted
pseudotable too.
It could be incorporated with your update
trigger using for update, delete
as your update trigger only includes the "before" values so the code for both would be the same currently.
Edit: Though then your trigger would need to check if EXISTS(SELECT * FROM inserted)
to determine the action string so maybe separate would be easier.
精彩评论