Change Tracking - 2008

  • Comments posted to this topic are about the item Change Tracking - 2008

  • Nice example of change tracking.

    You mention

    is designed to work with Sync Services for ADO.NET.

    but the article lacks the example or refs to do so.

    e.g. as a starting point:

    Change Tracking and Sync Services for ADO.NET

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution πŸ˜€

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • This is not a replacement to track complete history of changes (who dunnit) but more a 'do i have the latest version'-tool(replication/synchronisation)

    To track changes I usually add a trigger and a valid_time_table for important tables.

    A valid time table is a copy of the original table with these added columns

    ID_HistoriekintUnchecked Primary Key

    audit_StartTimedatetimeUnchecked -> timestamp of change

    audit_StartUservarchar(50)Unchecked

    audit_EndTimedatetimeChecked -> default '9/9/9999' -> timestamp of next change

    audit_EndUservarchar(50)Checked -> default null

    The basic layout of the trigger on the table

    IF UPDATE(field_you_want_to_track_changes_for)

    or update ()

    or not exists ( select 1 from hist_PDO_historiek where ID_PDO in (select ID_PDO from inserted)

    BEGIN

    UPDATE hist_PDO_Historiek

    SET audit_endtime = (@TrigTime),

    audit_enduser = (SUSER_SName())

    FROM deleted,

    hist_PDO_Historiek

    WHERE hist_PDO_Historiek.ID_PDO = deleted.ID_PDO

    AND audit_endtime = '9/9/9999'

    INSERT INTO dbo.hist_PDO_Historiek ([*All table fields you want a history off*],

    [audit_StartTime],

    [audit_EndTime],

    [audit_StartUser])

    SELECT [*All table fields you want a history off*],

    @TrigTime,

    '9/9/9999',

    Suser_Sname()

    FROM inserted

    END

    /*GOTCHA*/

    The IF(UPDATE) checks wether the field is mentioned in the update/insert/delete statement , NOT wether the field was actually changed

    UPDATE x set field=field from x -> value is not changed but update statement is logged

    There is no 'retention period' so without modification/custom cleanup everything remains logged

    TIP: You can also do a record based retention(last 10 modifications) instead of time based (last 3 days)

    Advantages over change_tracking?

    Username information (you could also store applicition/IP/...)

    Easier searching in the history (select * from history ...)

    You can reconstruct the values of the DB at a certain time since all history lines have a valid_from valid_to

    I'm sure change_tracking is more lightweight and causes less locking issues and probably takes less space (only changed values?) but it is not as easy to handle as a triggerbased valid_time_table solution

  • Koenraad Dendievel (6/18/2012)


    This is not a replacement to track complete history of changes (who dunnit) but more a 'do i have the latest version'-tool(replication/synchronisation)

    ...

    Roy Ernest[/url] published a great series of articles which discuss the different techniques SQLServer offers ...

    Allow me to refer to the last one ( because that one contains the refs to the previous articles :w00t: )

    What, when and who? Auditing 101 - Part 3[/url]

    CT only handles "it changed" not the who did it part.

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution πŸ˜€

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • In the example provided, the select from the change table following the DML below did not appear to reveal the insertion of the EmployeeId 2 or the update to EmpolyeeId 3. I'm only seeing 2 inserts and on delete.... Am I missing something? Thanks!

    INSERT INTO tblEmployee Values (1, Null, 'Mike', 'Fields', 1)

    INSERT INTO tblEmployee Values (2, 1, 'John', 'Hopkins', 1)

    INSERT INTO tblEmployee Values (3, 1, 'Henry', 'III', 1)

    Delete tblEmployee Where EmployeeID = 2

    Update tblEmployee Set FirstName = 'Suresh', LastName = 'Sankar' Where EmployeeID = 3

  • What edition of SQL Server is this available with?

  • all.

    have a look at Features Supported by the Editions of SQL Server 2008

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution πŸ˜€

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • In SQL Server 2008 you can use two prepared method to get the changes on a table. One is called as "Change Tracking" and the other is "Change Capturing". With change tracking option you can have the last chage about the record. By using change capturing you can have the history of the changes.

    In my project i decided to use the change tracking method. Becaus it is a internal process it is faster. And i don't need to manage change version etc. So, it was good choice in my opinion.

    I successfully applied the method to my system. Everything was working well unitill we need to restart our server to fix another isue. When we start the server back, we noticed that backup of the change tracking enabled databases were not working anymore. It was a bug with the chacnge tracking system. When you restart the sql server, cleanup process of the change tracking system is corrupted and some duplicate records remains in "syscommittab" system table. The message was

    β€œCannot insert duplicate key row in object 'sys.syscommittab' with unique index 'si_xdes_id'. [SQLSTATE 23000] (Error 2601) Failed to flush the commit table to disk in dbid ID due to error 2601. Check the errorlog for more information. [SQLSTATE 42000] (Error 3999) BACKUP DATABASE is terminating abnormally. [SQLSTATE 42000] (Error 3013) The statement has been terminated. [SQLSTATE 01000] (Error 3621). The step failed.”

    Workaround of this case is so troublesome. This is a known isue. Check the KB 978839. I applied the service pack 3. But it did it again. At the end, i removed the change tracking implementation. As you guess, it was not easy while the system is used.

    I don't suggest to use change tracking system in sql server 2008.

  • Excellent post. Thanks Nitesh.

  • First off, nice post, I will start playing with this some more. Also, I do agree with some of the others who have mentioned that this solution does not provide the "who dunnit and when" which is important for SOX or HIPPA auditing. I have used a solution in the past where we just implemented the same trigger on any table we wanted to audit. This trigger would "asynchronously" audit any data changes (even deletes) and publish them using the SQL Server Service Broker. The auditing database could then be centralized whereever we wanted, and if needed, all databases could publish "audits" to the SB channel, and it didn't matter if the auditing database moved to a different server in the future. Again, everything was Async so none of the ETL processing was slowed down. We did not create those god-awful "copy a table and its schema" solutions and just add the same records to them approach I see others doing.

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply