I created two triggers already:
create trigger [insert_history] on users
for insert
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
deletedpseudotable too.It could be incorporated with your
updatetrigger usingfor update, deleteas 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.