栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在不插入行的情况下获取下一个ID

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在不插入行的情况下获取下一个ID

编辑:

在花了几个小时比较整个页面转储之后,我意识到有一种更简单的方法,我应该留在DMV上。

该值在备份/还原后仍然有效,这清楚地表明了该值已存储-我转储了数据库中的所有页面,但是找不到添加记录时的位置/更改。比较20万行的页面转储并不是一件好事。

我使用了专用的管理控制台,对每个暴露的内部表都进行了转储,插入了一行,然后又对系统表进行了转储。这两个转储是相同的,这表明它可以生存,因此必须进行存储,即使在该级别也没有暴露。

因此,绕了一圈后,我意识到DMV确实有答案。

create table foo (MyID int identity not null, MyField char(10))insert into foo values ('test')go 10-- Inserted 10 rowsselect Convert(varchar(8),increment_value) as IncrementValue,   Convert(varchar(8),last_value) as LastValuefrom sys.identity_columns where name ='myid'-- insert another rowinsert into foo values ('test')-- check the values againselect Convert(varchar(8),increment_value) as IncrementValue,   Convert(varchar(8),last_value) as LastValuefrom sys.identity_columns where name ='myid'-- delete the rowsdelete from foo-- check the DMV againselect Convert(varchar(8),increment_value) as IncrementValue,   Convert(varchar(8),last_value) as LastValuefrom sys.identity_columns where name ='myid'-- value is currently 11 and increment is 1, so the next insert gets 12insert into foo values ('test')select * from fooResult:MyID        MyField----------- ----------12          test(1 row(s) affected)

仅仅因为行被删除,最后一个值没有被重置,所以最后一个值+增量应该是正确的答案。

也要在我的博客上写剧集。

哦,这是所有方法的捷径:

select ident_current('foo') + ident_incr('foo')

因此,事实证明这很容易-但这一切都假设在您找回ID时,没有其他人使用过您的ID。可以进行调查,但是我不想在代码中使用它。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/441452.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号