Note that there are some explanatory texts on larger screens.

plurals
  1. POAudit Triggers: Use INSERTED or DELETED system tables
    primarykey
    data
    text
    <p>The topic of how to audit tables has recently sprung up in our discussions... so I like your opinion on whats the best way to approach this. We have a mix of both the approaches (which is not good) in our database, as each previous DBA did what he/she believed was the right way. So we need to change them to follow any one model.</p> <pre><code>CREATE TABLE dbo.Sample( Name VARCHAR(20), ... ... Created_By VARCHAR(20), Created_On DATETIME, Modified_By VARCHAR(20), Modified_On DATETIME ) CREATE TABLE dbo.Audit_Sample( Name VARCHAR(20), ... ... Created_By VARCHAR(20), Created_On DATETIME, Modified_By VARCHAR(20), Modified_On DATETIME Audit_Type VARCHAR(1) NOT NULL Audited_Created_On DATETIME Audit_Created_By VARCHAR(50) ) </code></pre> <p><b>Approach 1:</b> Store, in audit tables, only those records that are replaced/deleted from the main table ( using system table DELETED). So for each UPDATE and DELETE in the main table, the record that is being replaced is INSERTED into the audit table with 'Audit_Type' column as wither 'U' ( for UPDATE ) or 'D' ( for DELETE)</p> <p>INSERTs are not Audited. For current version of any record you always query the main table. And for history you query audit table.</p> <p>Pros: Seems intutive, to store the previous versions of records Cons: If you need to know the history of a particular record, you need to join audit table with main table.</p> <p><b>Appraoch 2:</b> Store, in audit table, every record that goes into main table ( using system table INSERTED).</p> <p>Each record that is INSERTED/UPDATED/DELETED to main table is also stored in audit table. So when you insert a new record it is also inserted into audit table. When updated, the new version (from INSERTED) table is stored in Audit table. When deleted, old version (from DELETED) table is stored in audit table.</p> <p>Pros: If you need to know the history of a particular record, you have everything in one location.</p> <p>Though I did not list all of them here, each approach has its pros and cons?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload