简体   繁体   English

将更改历史记录保存到SQL Server 2008中的XML字段

[英]Saving change history to XML field in SQL Server 2008

I have worked on several applications that saved XML data into a database. 我曾经研究过几种将XML数据保存到数据库中的应用程序。 To date all of them simply overwrote the current XML contents with the new XML contents. 迄今为止,它们都只是用新的XML内容覆盖了当前的XML内容。

I would like to save the changes to the XML to the database but not overwrite what is currently in the DB. 我想将对XML的更改保存到数据库中,但不覆盖数据库中当前的内容。

Short of just creating new rows for changes and leaving the old rows intact, what other options could I use? 除了仅为更改创建新行并完整保留旧行之外,我还可以使用其他哪些选项?

I've thought about just saving the differences as actual text differences, which does not always work for XML. 我曾经考虑过将差异保存为实际的文本差异,但这并不总是适用于XML。

Am I nuts for even wanting this? 我甚至想要这个也疯了吗?

You may want to consider adding an audit table to sit beside your data table. 您可能需要考虑将审核表添加到数据表旁边。

It would have only the columns you are interested in tracking the history of along with a datetime column (to record the date of change) and normally you want some user information as well (ie pull the logged in user from the connection or provide a user name in your insert/update). 它只会包含您感兴趣的用于跟踪历史记录的列以及一个datetime列(以记录更改日期),并且通常您还需要一些用户信息(例如,从连接中拉出已登录的用户或提供一个用户)插入/更新中的名称)。

Then create an Update trigger on your data table and insert new rows into your audit table from the deleted pseudo table. 然后在数据表上创建更新触发器,然后将新行从已删除的伪表插入到审核表中。 This will contain the data prior to an update if you are performing an update since an update is really a delete and an insert. 如果您要执行更新,则它将包含更新之前的数据,因为更新实际上是删除和插入。

There is also this more general purpose solution that proposes to use SQL CLR to abstract away implementing a unique audit table and trigger for each data table you wish to audit: http://www.sqljunkies.ddj.com/Article/4CD01686-5178-490C-A90A-5AEEF5E35915.scuk 还有一个更通用的解决方案,建议使用SQL CLR抽象化实现唯一的审核表并为要审核的每个数据表触发: http : //www.sqljunkies.ddj.com/Article/4CD01686-5178 -490C-A90A-5AEEF5E35915.scuk

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM