且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用自动增量设置列的起始值

更新时间:2022-10-23 21:49:56

来自 重置 SQL Server 标识列:

检索表 Employees 的标识:

DBCC checkident ('Employees')

修复身份种子(如果由于某种原因数据库正在插入重复的身份):

DBCC checkident ('Employees', reseed)

将表 Employees 的身份种子更改为 1000:

DBCC checkident ('Employees', reseed, 1000)

插入的下一行将从 1001 开始.

I have a table Orders with the following fields:

Id | SubTotal | Tax | Shipping | DateCreated

The Id column is set to autoincrement(1,1).

This is to be used in an E-commerce storefront. Sometimes a current E-commerce store is migrated to my platform and they already have orders - which could mean that their current Order.Id is, for example, 9586.

I want to have the autoincrement field start from that value.

How can I do this?

From Resetting SQL Server Identity Columns:

Retrieving the identity for the table Employees:

DBCC checkident ('Employees')

Repairing the identity seed (if for some reason the database is inserting duplicate identities):

DBCC checkident ('Employees', reseed)

Changing the identity seed for the table Employees to 1000:

DBCC checkident ('Employees', reseed, 1000)

The next row inserted will begin at 1001.