如何获取SQL Server 2008中特定表的所有事务日志(插入更新删除)
我想获取 SQL Server 2008 中特定表上应用的所有事务。
我找到了使用此脚本更新表的最后一次:
SELECT OBJECT_NAME(OBJECT_ID) AS DatabaseName, last_user_update,*
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID( 'DBName')
AND OBJECT_ID=OBJECT_ID('tableName')
我想知道该表的所有事务(插入、更新、删除)及其日期时间和应用的查询。
最好的方法是什么?
I want to get all the transactions applied on a specific table in SQL Server 2008.
I found the last time a table was updated using this script:
SELECT OBJECT_NAME(OBJECT_ID) AS DatabaseName, last_user_update,*
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID( 'DBName')
AND OBJECT_ID=OBJECT_ID('tableName')
I want to know all the transactions (Inserts, Updates, Deletes) for that table, and their datetime, and the query applied.
What is the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在合理的时间内做到这一点的唯一方法是使用第三方工具(正如马丁在第一条评论中所说),例如 ApexSQL Log,可以读取事务日志并获取您需要的信息。
请注意,为了使其正常工作,您的数据库必须处于完全恢复模式,因为此时 SQL Server 会记录完整的事务详细信息,以便稍后重建。
另一种选择是研究如何使用未记录的 fn_dblog 函数,但这会花费您更多的时间,并且您将无法读取分离的日志或事务日志备份。
The only way to do this in a reasonable amount of time is to use a third party tool(as Martin said in first comment) such as ApexSQL Log that can read transaction log and get the information you need.
Note that in order for this to work your database has to be in a full recovery mode because that’s when SQL Server logs full transaction details that can be reconstructed later.
Another option is to investigate how to use undocumented fn_dblog function but this will take you a lot more time and you won’t be able to read detached logs or transaction log backups.
创建一个触发器,该触发器将创建一个新表 Emp_audit 并在对表员工进行任何更改时向其中添加新元组
creating a trigger which will create a new table Emp_audit and add new tuples to it whenever any change is made to table employee
您最好针对此需求设置审核,而不是尝试从事务日志中追溯提取此信息。
如果您使用的是企业版,则可以使用内置的 SQL Server Audit 功能,否则通过触发器记录所需信息应该相对简单。
You would be much better off setting up auditing for this need rather than trying to extract this information retrospectively from the transaction log.
If you are on Enterprise Edition you could use the built in SQL Server Audit functionality, otherwise it should be relative straight forward to log the desired information via triggers.
您可以创建自己的事务日志
步骤 1:为事务日志创建自己的表
步骤 2:创建创建日志的存储过程。 (注意:将 YourTablePKColumn 替换为您的表主键列。)
第 3 步:为您想要拥有事务日志的表创建触发器
You could create your own transaction logs
Step 1: Create your own table for transaction logs
Step 2: Create stored procedure that create logs. (Note: Replace YourTablePKColumn with your table primary key column.)
Step 3: Created trigger to table you want to have transaction logs