查找表

时间:2016-09-01 11:04:43

标签: sql sql-server

我有表queue的值。

id name
1  apple
2  mango
3  banana  -- (I assumed this name, don't remember it exactly)

但我不小心将其改为

id name
1  apple
2  mango
3  John

所以我把它还给了

id name
1  apple
2  mango
3  banana

但现在知道id=3的名字不是香蕉。在改变它之前,我怎么知道那里曾经是什么?

1 个答案:

答案 0 :(得分:2)

如果您的数据库处于完全恢复模式,您可以阅读事务日志。

演示如下。

create table test4
(
name varchar(10)
)

insert into test4
select 'abc'
union all
select 'def'

---updated mistakenly
update test4
set name='def' where name='abc'

select [Current LSN], [Operation], [Transaction ID], 
    [Transaction SID], [Begin Time], [End Time],
    [Num Elements], [RowLog Contents 0], [RowLog Contents 1], [RowLog Contents 2],
    [RowLog Contents 3], [RowLog Contents 4]
    from fn_dblog(null, null)
    where  [Transaction ID]='0000:00050df7'


    select cast(0x616263 as varchar(20))--'abc--rowlogcontent0 value

需要注意的几点:
1.您可以在Tlog中找到确切的表格,并通过查询以下来查找更新

select * from fn_dblog(null,null)
    where  allocunitname='name of your table' and operation='LOP_MODIFY_ROW'

2.对于更新Rowlogcontent0将包含图像之前,rowlogcontent1将包含图像之后

参考文献和进一步阅读:
http://rusanu.com/2014/03/10/how-to-read-and-interpret-the-sql-server-log/