基于SQL Server日志链查看数据库insert/update/delete操作(一)

在MSSQLServer2008下的语句 不同版本可能语句会有微小差别

 1 SELECT 
 2 [Slot ID], [Transaction ID], Operation,
 3 AllocUnitName,
 4 [Current LSN], 
 5 [Log record] ,
 6 [RowLog Contents 0],
 7 [RowLog Contents 1],
 8 [RowLog Contents 2],
 9 [RowLog Contents 3],
10 [RowLog Contents 4], 
11 [Log Record Fixed Length],
12 [Log Record Length], 
13 [Context],
14 AllocUnitId, 
15 [Page ID] 
16 FROM sys.fn_dblog(NULL,NULL)
17 WHERE AllocUnitName ='dbo.Table_2'
18 AND Context IN ('LCX_MARK_AS_GHOST', 'LCX_HEAP', 'LCX_CLUSTERED') 
19 AND Operation IN ('LOP_DELETE_ROWS', 'LOP_INSERT_ROWS','LOP_MODIFY_ROW')
View Code

 

 

查询结果后 需要解析数据。解析数据需要对应表中的字段类型,所以要先知道表的具体结构(字段类型、长度等),根据字段类型匹配相应的value.

一般情况下会用到

[RowLog Contents 0],
[RowLog Contents 1],
[RowLog Contents 2]

三个字段的值,insert操作的数据会在[RowLog Contents 0]中,update操作会在[RowLog Contents 0],[RowLog Contents 1]中,delete操作的数据在[RowLog Contents 0]。

[RowLog Contents 2]会存储表的ID信息,整个所有的信息都会存储在[Log record]字段中。现在的问题是这些字段存储的是十六进制,需要解析,微软有一个存储格式的说明,后面的文章会把解析过程分享一下,目前正在积极的实现解析过程。

 

posted @ 2015-01-21 09:27  预见者  阅读(4114)  评论(1编辑  收藏  举报