i use sql server 2005 , have below question:
on table a
, have trigger tracks insert/update/delete it. tracked records inserted in audit table (aaudit
). when run update on a
, seeing 2 rows in audit table each update, not expect. here trigger have defined:
alter trigger [atrigger] on [dbo].[a] insert, update, delete insert [dbo].[aaudit] ([businessdate], [datatypeid], [bookid], [version], [delflag], [auditdate], [extstatus]) select [businessdate], [datatypeid], [bookid], [version], 'n', getdate(), 0 inserted insert [dbo].[aaudit] ([businessdate], [datatypeid], [bookid], [version], [delflag], [auditdate], [extstatus]) select [businessdate], [datatypeid], [bookid], [version], 'y', getdate(), 0 deleted
why above trigger resulting in 1 row delflag = 'y'
, 1 row delfalg = 'n'
in audit table?
thanks taking @ question.
vikram
in order separate 3 operations insert, update, delete, need additional checks:
alter trigger [atrigger] on [dbo].[a] insert, update, delete -- true inserts - (id) primary key *not* present in "deleted" table insert [dbo].[aaudit]([businessdate], [datatypeid], [bookid], [version], [delflag], [auditdate], [extstatus]) select [businessdate], [datatypeid], [bookid], [version], 'n', getdate(), 0 inserted (id) not in (select distinct (id) deleted) -- true deletes - (id) primary key *not* present in "inserted" table insert [dbo].[aaudit]([businessdate], [datatypeid], [bookid], [version], [delflag], [auditdate], [extstatus]) select [businessdate], [datatypeid], [bookid], [version], 'y', getdate(), 0 deleted (id) not in (select distinct (id) inserted) -- updates - (id) primary key present in both "inserted" , "deleted" table insert [dbo].[aaudit]([businessdate], [datatypeid], [bookid], [version], [delflag], [auditdate], [extstatus]) select [businessdate], [datatypeid], [bookid], [version], 'n', getdate(), 0 inserted inner join deleted d on i.id = d.id
Comments
Post a Comment