SQLUSA
BI TRIO 2008
FREE TRIAL
CLICK HERE TO ORDER

SQL Server 2008 Best Practices
SQL Server 2005 Best Practices

How to architect a trigger on column update?

Execute the following SQL Server T-SQL script in Query Editor to create a trigger which fires when Salary or SSN is updated.

The trigger creates two (2) audit trail entries: previous data and new data(replacing data).

-- SQL insert audit trail table when sensitive data changes

-- SQL after trigger - SQL trigger - SQL column update trigger

USE Payroll

GO

 

CREATE TRIGGER trgEmployeeUpdate

ON Employee

AFTER UPDATE

AS

  IF (Update(Salary)

       OR Update(SSN))

    BEGIN

      INSERT INTO CorpSecurityEmployeeHistory

                 (auditlogtype,

                  auditEmployeeDeptID,

                  auditEmployeeID,

                  auditEmployeeSalary,

                  auditEmployeeSSN,

                  auditUpdatedBy)

      SELECT 'PREVIOUSDATA',

             DeptID,

             EmployeeID,

             Salary,

             SSN,

             User_name()

      FROM   DELETED

      

      INSERT INTO CorpSecurityEmployeeHistory

                 (auditlogtype,

                  auditEmployeeDeptID,

                  auditEmployeeID,

                  auditEmployeeSalary,

                  auditEmployeeSSN,

                  auditUpdatedBy)

      SELECT 'NEWDATA',

             DeptID,

             EmployeeID,

             Salary,

             SSN,

             User_name()

      FROM   INSERTED

    END;

 

GO

 

The World Leader in SQL Server 2008 Training
The future is just a CLICK away. Your future!
 
SQLUSA.com Home Page

Copyright 2005-2010, SMI Corp. All Rights Reserved.

SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.