读书人

查看SQL Server日记 Part 1

发布时间: 2012-08-25 10:06:20 作者: rapoo

查看SQL Server日志 Part 1

曾经有朋友问我数据被删除了,不借助第三方工具能不能查是什么时候发生的。 SQL Server提供了一个undocumented的函数fn_dblog可以让我们查看活动的transaction log。

语法如下:

::fn_dblog(@StartingLSN,@EndingLSN)

如果参数都为NULL默认是抓取所有的交易信息。

使用这个函数我们可以查询DML,DDL信息,比如数据删除,修改更新等等。下面我们来看一个数据更新的例子:

create table test(namevarchar(10))

--插入条数据

insert into testvalues('allen test')

go 5


---查询对表Test的修改

select [Transaction Name],Operation,AllocUnitId,AllocUnitName,[Begin Time]fromfn_dblog(null,null)

where AllocUnitName ='dbo.test'andOperation='LOP_INSERT_ROWS'

----------------------------------------------------------------------------------------------------------------------------------------------------

NULLLOP_INSERT_ROWS72057594040090624 dbo.test NULL

NULLLOP_INSERT_ROWS72057594040090624 dbo.test NULL

NULLLOP_INSERT_ROWS72057594040090624 dbo.test NULL

NULLLOP_INSERT_ROWS72057594040090624 dbo.test NULL

NULLLOP_INSERT_ROWS72057594040090624 dbo.test

--删除表

drop table test

----从Log中查询对表的删除

select [Transaction Name],Operation,AllocUnitId,AllocUnitName,[Begin Time]fromfn_dblog(null,null)

where [Transaction Name] = 'DROPOBJ'

----------------------------------------------------------------------------------------------------------------------------------------------------

DROPOBJLOP_BEGIN_XACT NULL NULL

--查询Page Split

select Operation, AllocUnitName,COUNT(*)asNumberofIncidents

from ::fn_dblog(null,null)

where Operation =N'LOP_DELETE_SPLIT'

group byOperation, AllocUnitName

注意:从fn_dblog 查出来的LSN是不能直接作为参数的,需要将16进制转化为numeric:

The LSN we havefrom the log dump above is 0000009d:0000021e:0001.To convert it:

读书人网 >SQL Server

热点推荐