在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')
查詢結果后 需要解析數據。解析數據需要對應表中的字段類型,所以要先知道表的具體結構(字段類型、長度等),根據字段類型匹配相應的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]字段中。現在的問題是這些字段存儲的是十六進制,需要解析,微軟有一個存儲格式的說明,后面的文章會把解析過程分享一下,目前正在積極的實現解析過程。