編輯:
花費了一些比較完整的頁面轉儲小時後,我意識到有一個更簡單的方法,我應該的留在動態管理視圖。
值存在備份/恢復,這是一個清楚的指示,它被存儲 - 我傾銷所有頁面的數據庫,並找不到位置/更改時添加記錄。比較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