在SQL Server中重置标识列值

If you are using an identity column on your SQL Server tables, you can set the next insert value to whatever value you want. An example is if you wanted to start numbering your ID column at 1000 instead of 1.

如果在SQL Server表上使用标识列,则可以将下一个插入值设置为所需的任何值。 例如,如果您想将ID列的编号从1000开始而不是1。

It would be wise to first check what the current identify value is. We can use this command to do so:

首先检查当前标识值是明智的。 我们可以使用以下命令来这样做:

DBCC CHECKIDENT (‘tablename’, NORESEED)

DBCC CHECKIDENT(“表名”,已删除)

For instance, if I wanted to check the next ID value of my orders table, I could use this command:

例如,如果我想检查订单表的下一个ID值,则可以使用以下命令:

DBCC CHECKIDENT (orders, NORESEED)

DBCC CHECKIDENT(订单,已删除)

To set the value of the next ID to be 1000, I can use this command:

要将下一个ID的值设置为1000,我可以使用以下命令:

DBCC CHECKIDENT (orders, RESEED, 999)

DBCC CHECKIDENT(订单,RESEED,999)

Note that the next value will be whatever you reseed with + 1, so in this case I set it to 999 so that the next value will be 1000.

请注意,下一个值将是您使用+ 1重新设定的值,因此在这种情况下,我将其设置为999,以便下一个值将是1000。

Another thing to note is that you may need to enclose the table name in single quotes or square brackets if you are referencing by a full path, or if your table name has spaces in it. (which it really shouldn’t)

要注意的另一件事是,如果要通过完整路径引用表名称,或者表名称中包含空格,则可能需要将表名称括在单引号或方括号中。 (这确实不应该)

DBCC CHECKIDENT ( ‘databasename.dbo.orders’,RESEED, 999)

DBCC CHECKIDENT('databasename.dbo.orders',RESEED,999)

翻译自: https://www.howtogeek.com/howto/database/reset-identity-column-value-in-sql-server/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值