简体   繁体   English

SQL Server-跟踪/记录用户所做的更改

[英]SQL Server - Tracking/Logging changes made by users

We have a program which consists of client(multiple) -> gateway -> engine ... Now the customers would like a feature, which would enable them to see what and by who certain change was made, so we would need to save changes to some history table. 我们有一个程序,该程序由客户端(多个)->网关->引擎组成...现在,客户想要一个功能,该功能使他们能够查看所做的某些更改以及由谁进行某些更改,因此我们需要保存更改到一些历史记录表。

PROBLEM: 问题:

Because there could be multiple users connected to engine in parallel, how can we know, if we would implement history tracking using triggers, which user made certain change, how can we identify it in sql server (in some previous program, where connection to sql was sequential, they save, before processing, user id to a table and trigger used that id). 因为可能有多个用户并行连接到引擎,所以我们如何知道是否要使用触发器来实现历史记录跟踪,哪个用户进行了某些更改,如何在sql server中识别它(在以前的某个程序中,该连接到sql是顺序的,他们在处理之前将用户ID保存到表中,并使用该ID触发)。

Regarding connection to SQL Server - some things are implemented using EF, some SP and some using dynamic query. 关于与SQL Server的连接-有些事情是使用EF实现的,有些则是使用SP进行的,有些则使用动态查询来实现。

  • CDC is not an option, because not all customers posses enterprise version and even there some "hacking" would have to take place in order to have it work. CDC不是一种选择,因为并非所有客户都拥有企业版,甚至为了使它起作用,也必须进行一些“黑客攻击”。

  • I read that CONTEXT_INFO could be used to save current user id to it and then in sql read that value from ... has anyone tried that? 我读到CONTEXT_INFO可以用来保存当前的用户ID,然后在sql中从...读取该值。有人尝试过吗?

  • Some even add columns to tables like modified date and user id, but we would like to avoid that if possible. 有些人甚至在表格中添加了列,例如修改日期和用户ID,但我们希望尽可能避免这种情况。

So ... what would be the best way to implement this? 那么...实现此目标的最佳方法是什么? :) :)

First of all this could drastically impact your database performance and disk space... 首先,这可能会严重影响您的数据库性能和磁盘空间。

But if you insist, you can figure a simple script that iterates over the list of all tables you want to monitor and dynamically create (via sp_executesql) the associated trigger. 但是,如果您坚持要这样做,则可以找到一个简单的脚本,该脚本在要监视的所有表的列表上进行迭代,并通过sp_executesql动态创建关联的触发器。

Then having a name convention of those triggers might help you later to maintain them (eg to disable all of them while data intensive operations, etc). 然后使用这些触发器的名称约定可能会在以后帮助您维护它们(例如,在数据密集型操作中禁用所有触发器等)。

I would suggest to try to convince your client to activate this feature only for few critical data tables. 我建议尝试说服您的客户端仅对少数关键数据表激活此功能。 Also, always keep at hand a script that deactivate all those triggers. 同样,请始终准备好一个脚本,以停用所有这些触发器。

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

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