如何查看自增长以及重置自增长

how to get the next autoincrement value in sql

To get the next auto-increment value from SQLServer :

This will fetch the present auto-increment value.

SELECT IDENT_CURRENT('table_name');

Next auto-increment value.

SELECT IDENT_CURRENT('table_name')+1;

------> This will work even if you add a row and then delete it because IDENT_CURRENT returns the last identity value generated for a specific table in any session and any scope.

 

反驳

  This is just plain not true - the IDENTITY could have an increment of something different than 1 ! Fact is: you CANNOT get the next value for an IDENTITY column in SQL Server - there is no way to do this. The values only get set when the INSERT happens - not before. – marc_s Jul 13 '12 at 11:12

 

   @patel.milanb: the increment vale of your IDENTITY could be five - so your next value would be 1515 (and NOT 1511!) - and also: if the next insert is rolled back, a value (like 1515) might be "lost" - and the real next value ends up being 1520. Just adding +1 to the IDENT_CURRENT is a GUESS at best..... – marc_s Jul 13 '12 at 11:19

 

 

Reset AutoIncrement in SQL Server after Delete

Issue the following command to reseed mytable to start at 1:

DBCC CHECKIDENT (mytable, RESEED, 0)

Read about it in the Books on Line (BOL, SQL help). Also be careful that you don't have records higher than the seed you are setting.

 

转载于:https://www.cnblogs.com/chucklu/p/10831876.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值