Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>We chose separate tables as it will keep your primary table lean and faster. We also opted for triggers, reasoning that if you ever change the entry mechanism, you wouldn't want to have to rewrite your auditing. Also it can capture accidental DBA side changes.</p> <p>Since update is effectively a delete and then an insert, you can achieve what has been suggested with a single trigger- this is what we did.</p> <ul> <li><p>Create a table exactly matching your existing table, but with some added columns: AUDIT_GUID VARCHAR(40), AUDIT_TIMESTAMP DATETIME, AUDIT_ACTION VARCHAR(20)</p></li> <li><p>Create an "AFTER INSERT,DELETE,UPDATE" Trigger using the following general pattern (simply add more columns where necessary).</p> <pre><code>CREATE TRIGGER CustomerAudit ON Customer AFTER INSERT,DELETE,UPDATE AS BEGIN IF (TRIGGER_NESTLEVEL()&gt;1) RETURN DECLARE @Time DateTime = CURRENT_TIMESTAMP DECLARE @Audit_GUID varchar(100) = NEWID() INSERT INTO Customer_History (FirstName, LastName, Audit_Date, Audit_Action, Audit_GUID) SELECT FirstName, LastName, @Time, 'Delete', @Audit_GUID FROM Deleted INSERT INTO Customer_History (FirstName, LastName, Audit_Date, Audit_Action, Audit_GUID) SELECT FirstName, LastName, @Time, 'Insert', @Audit_GUID FROM Inserted END </code></pre></li> </ul> <p>If you want to find updates, they will be rows in the history table that have a delete &amp; update with the same Audit_GUID value. The timestamp also allows you to check changes made at a certain time, and we've also added a currentuser to find the person to blame if necessary!</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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