I have a table and want to insert the current values of records that are being updated or deleted.
I tried to create a trigger on before update and on before delete but SQL Server 2005 doesn’t like the before word.
How do I get this to work?
I want the trigger to take the current record and copy it to a history table that is a mirror of the base table with the two added fields of dateBackedUp (that takes a getDate()) and a field called action (that takes the action ‘updated’ or deleted’)
please help
SQL Server doesn’t have a concept of
BEFORE INSERTorBEFORE DELETE. These kind of audit operations really don’t need to be executed before the action, most of the time – so just use theAFTER INSERTandAFTER UPDATEtriggers that you already have.When you do an
AFTER UPDATEtrigger, theInsertedpseudo-table contains the new values – and theDeletedtable contains the old values. So for theAFTER UPDATEtrigger, you can access both pseudo tables and get both the old and the new values.The
AFTER INSERTtrigger only has access toInsertedpseudo table which contains the values that were inserted.The
AFTER DELETEtrigger only has access to theDeletedpseudo table which contains the values of the rows that were deleted.