• SteveEClarke (1/29/2013)


    Sean Lange (1/28/2013)


    SteveEClarke (1/28/2013)


    I have managed to answer my own question - through a little digging -

    Using the DELETED table as the before data.

    New Trigger;

    ALTER TRIGGER [dbo].[trgGenMasterUpdate]

    ON [dbo].[GenMaster]

    AFTER UPDATE

    AS

    BEGIN

    SET NOCOUNT ON;

    insert into GenMasterUpdates (GlCode, Action, Before, After )

    select INSERTED.GlCode, 'UPDATE', DELETED.Description, INSERTED.Description

    from INSERTED join GenMaster on INSERTED.GlCode = GenMaster.GlCode

    join DELETED on INSERTED.GlCode = DELETED.GlCode

    END

    Assuming you have some way of knowing when the update is made (like a datetime column for UpdateDate) you really don't need the After data. The after data will be in the next audit row (assuming you order them), if there is no previous row the next value will be what is currently in the table. This will cut your auditing storage in half. ๐Ÿ™‚

    Yes - I agree - the example I posted was a very simple one just to get me and others thinking -

    The final table has the following columns ;

    DATE

    TIME

    COLUMN_Updated

    BEFORE

    AFTER

    OPERATOR

    I have since put this script live and is working exactly as I had hoped.

    I appreciate your input though.

    Just my 2ยข again but I would not recommend using an EAV style of audit table. What I mean by that is you have the value which now will have to be stored as nvarchar regardless of the original datatype. This will cause you challenges at some point. But the bigger challenge is trying to figure out what any given row contained at a given point in time. With the way you are doing this you may quickly get millions of rows in this audit table. Let's say you have 50 columns and you routinely update 40 of them. You will have 40 rows in your audit table for each update, and each row is representing a column in the original table.

    Consider this, management walks in and says I know you audit this table now so can you please get me the contents from December 14th 2012 at 8:36am. How are you going to retrieve that data with this style of table?

    I find it much easier to just record the whole row on update. Then all you have to do for a point in time is find the most recent rows prior to the requested time.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 โ€“ Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/