Tracking changes in a SQL server 2005 database

前端 未结 8 1542
既然无缘
既然无缘 2020-12-29 00:05

I have been tasked with developing a solution that tracks changes to a database.

For updates I need to capture:

  • date of update
  • old value
8条回答
  •  遥遥无期
    2020-12-29 00:31

    One way I've seen this handled (though I wouldn't recommend it, honestly) is to handle it via stored procedures, passing in the userid/username/whatever as a parameter. The stored procedures would call a logging procedure, which wrote the relevant details in a central log table.

    Here's where it got a bit whacky, though...

    For INSERTs/UPDATEs, the relevant row(s) were stored in the table as XML data once the INSERT/UPDATE had completed successfully. For DELETEs, the row was stored prior to the DELETE running (though, realistically, they could have gotten it from the DELETE statement's output -- at least with SQL Server 2005).

    If I remember correctly, the table only had a couple columns: UserID, DateTime of the logging, Transaction Type (I/U/D), XML data containing the relevant rows, table name, and primary key value (mainly used for quick searching of what records they wanted).

    Many ways to skin a cat, though...

    My advice is to keep is simple. Expand it out later if/when you need to.

    If you have the ability to do so, lock down users to only be able to perform actionable statements on tables via stored procedures and then handle the logging (however you want) from there.

提交回复
热议问题