Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Trigger: insert, update, or delete

Author  Topic 

Zath
Constraint Violating Yak Guru

298 Posts

Posted - 2009-05-18 : 15:35:46

I need to be able to code for different actions in one trigger.
Is there a way to be able to tell if it's an update, insert, or delete?

I can do the FROM inserted for an update or insert (new records).
And I can do the FROM deleted for a delete or update (old records).

What I need is to add a new record to an audit table,
an updated record to an audit table (the updated record,
not the old one), and add a deleted record to the audit table.

I can easily add this code to each of the 3 stored procedures but
would like to put in a trigger. (Actually update an existing one)

Thanks for suggestions,

Zath

AjarnMark
SQL Slashing Gunting Master

3246 Posts

Posted - 2009-05-18 : 15:43:15
If there is a record in the inserted table, but not the deleted table for the PK, then the action was an insert.
If there is a record in the deleted table, but not the inserted table for the PK, then the action was an delete.
If there is a record in both tables for the PK, then the action was an update.

--------------------------------------------
Brand yourself at EmeraldCityDomains.com
Go to Top of Page

Zath
Constraint Violating Yak Guru

298 Posts

Posted - 2009-05-18 : 15:54:32
Ok, that makes sense. I can work with that.

Thanks!

Zath
Go to Top of Page
   

- Advertisement -