首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

查看SQL Server日记 Part 1

2012-08-25 
查看SQL Server日志Part 1曾经有朋友问我数据被删除了,不借助第三方工具能不能查是什么时候发生的。 SQL Se

查看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:

    Take the rightmost 4 characters (2-byte log record     number) and convert to a 5-character decimal number, including leading     zeroes, to get stringATake the middle number (4-byte log block number) and     convert to a 10-character decimal number, including leading zeroes, to get     stringBTake the leftmost number (4-byte VLF sequence number)     and convert to a decimal number, with no leading zeroes, to get stringCThe LSN string we need is stringC + stringB + stringA

    So0000009d:0000021e:0001 becomes '157' + '0000000542'+ '00001' ='157000000054200001'.(来自SQLSkills)

     

    可以参考下面的脚本:

     

    DECLARE @pageID$ NVARCHAR(23), @pageIDNVARCHAR(50), @sqlCmdNVARCHAR(4000);

    SET @pageID$ ='0001:0000004d'--- PageID

     

    SELECT @pageID =

    CONVERT(VARCHAR(4),CONVERT(INT,CONVERT(VARBINARY,

    SUBSTRING(@pageID$, 0, 5), 2)))

    + ',' +

    CONVERT(VARCHAR(8),CONVERT(INT,CONVERT(VARBINARY,

    SUBSTRING(@pageID$, 6, 8), 2)))

     

    ---查看Page内容

    SET @sqlCmd = 'DBCC PAGE (''Crack_Me'','+ @pageID + ',3) WITH TABLERESULTS'

    EXECUTE(@sqlCmd)

    另外这个函数只能对当前活动的Log生效,如果Log备份之后就无法读取了,我们需要用另外一个函数(fn_dump_dblog)从备份中读取。上面也只是能够看到数据被修改,但是无法做到恢复数据。

    目前网上有人做了一些脚本可以实现恢复,我现在正在做测试,如果能够成功的话后面会把脚本发出来。

     

     

热点排行