编辑:
花费了一些比较完整的页面转储小时后,我意识到有一个更简单的方法,我应该的留在动态管理视图。
值存在备份/恢复,这是一个清楚的指示,它被存储 - 我倾销所有页面的数据库,并找不到位置/更改时添加记录。比较200k行转储的页面并不好玩。
我曾经使用过专用的管理控制台,我把每个暴露的内部表的一个转储插入一行,然后进一步转储系统表。这两个转储点都是相同的,这表明虽然它存活了,并且因此必须存储,但即使在该级别也不会暴露。
因此,经过一个圈子后,我意识到DMV确实有答案。
create table foo (MyID int identity not null, MyField char(10))
insert into foo values ('test')
go 10
-- Inserted 10 rows
select Convert(varchar(8),increment_value) as IncrementValue,
Convert(varchar(8),last_value) as LastValue
from sys.identity_columns where name ='myid'
-- insert another row
insert into foo values ('test')
-- check the values again
select Convert(varchar(8),increment_value) as IncrementValue,
Convert(varchar(8),last_value) as LastValue
from sys.identity_columns where name ='myid'
-- delete the rows
delete from foo
-- check the DMV again
select Convert(varchar(8),increment_value) as IncrementValue,
Convert(varchar(8),last_value) as LastValue
from sys.identity_columns where name ='myid'
-- value is currently 11 and increment is 1, so the next insert gets 12
insert into foo values ('test')
select * from foo
Result:
MyID MyField
----------- ----------
12 test
(1 row(s) affected)
只是因为行得到去除,最后的值没有重置,所以最后值+增量应该是正确的答案。
也要写我的博客上的情节。
哦,抄近路到这一切:
select ident_current('foo') + ident_incr('foo')
所以它实际上原来是很容易 - 但是这一切都假定没有其他人使用你的ID,而你恢复了过来。很好的调查,但我不想在代码中使用它。
事务不会回滚身份计数器,并且总会有一些其他线程使用您认为稍后会获得的数字的风险。 – idstam 2009-10-09 11:21:26
感谢此@idstam 我确实怀疑这一点。 – iWeasel 2009-10-09 14:06:59