DBCC checkident resets the database ID column from a certain value

Source: Internet
Author: User

DBCC checkident (N 'dbo. Orders ', reseed, 0 );

DBCC checkident syntax
DBCC checkident
(
Table_name
[, {Noreseed | {reseed [, new_reseed_value]}]
)
[With no_infomsgs] Parameter
Table_name
Is the name of the table whose current identity value is to be checked. The specified table must contain an ID column. The table name must comply with the identifier rules.
Noreseed
Specifies that the current ID value should not be changed.
Reseed
Specifies that the current ID value should be changed.
New_reseed_value
The new value used to identify the current value of the column.
With no_infomsgs
Undisplay all information messages.
Result set
Whether or not any option is specified for the table containing the ID column, DBCC checkident returns the following information (the return value may be different ):
Checking identity information: Current identity value '20160301', current column value '20160301 '.
DBCC execution completed. If DBCC printed error messages, contact your system administrator. Note
The specific correction to the current Identifier value depends on the parameter specification.
DBCC checkident command identifier correction or correction
DBCC checkident (table_name, noreseed)
The current ID value is not reset. DBCC checkident returns the current ID value and the current maximum value of the ID column. If these two values are different, the ID value should be reset to avoid potential errors or gaps in the value sequence.
DBCC checkident (table_name)
Or
DBCC checkident (table_name, reseed)
If the current identity value of the table is smaller than the maximum identity value stored in the identity column, reset it with the maximum value in the identity column.
DBCC checkident (table_name, reseed, new_reseed_value)
Set the current ID value to new_reseed_value. If no rows have been inserted into the table since the table was created, or all rows have been deleted using the truncate TABLE statement, new_reseed_value is used as the identifier for the first row inserted after DBCC checkident is run. Otherwise, new_reseed_value + current increment value will be used for the next row to be inserted.
If this table is not empty, when you set the value of the table to a number smaller than the maximum value in the column, one of the following situations will occur:
If the primary key or unique constraint exists in the ID column, error 2627 is generated when the insert operation is performed in the table, because the generated id value conflicts with the existing value.
If the primary key or unique constraint does not exist, subsequent insert operations generate duplicate ID values.
Exception
The following table lists the conditions when DBCC checkident does not automatically reset the current identity value, and provides methods to reset this value.
Condition resetting Method
The current identity value is greater than the maximum value in the table.
Run DBCC checkident (table_name, noreseed) to determine the current maximum value in the column and specify this value as new_reseed_value in DBCC checkident (table_name, reseed, new_reseed_value.
Or
Run DBCC checkident (table_name, reseed, new_reseed_value) When new_reseed_value is set to a very low value, and then run DBCC checkident (table_name, reseed) to correct the value.
Delete all rows in the table.
Run DBCC checkident (table_name, reseed, new_reseed_value) When new_reseed_value is set to the required start value ).
Change seed value
The seed value is the value inserted to the ID column in the first row of the loaded table. All subsequent rows contain the current ID value and increment value. The current ID value is the latest id value generated for the current table or view. For more information, see Create and modify an identifier column.
You cannot use DBCC checkident to execute the following tasks:
Change the original seed value specified for the column when creating a table or view.
Reset the seed value of an existing row in a table or view.
To change the original seed value and reset the seed value of all existing rows, you must delete and recreate the ID column and then specify a new seed value for the ID column. When a table contains data, it also adds the identification number to an existing row with the specified seed value and increment value. The row update sequence cannot be guaranteed.
Permission
The caller must have a table or a member of the SysAdmin fixed server role, db_owner fixed database role, or db_ddladmin fixed database role.
Example
A. Reset the current ID value as needed
The following example resets the current ID value of the employee table in the adventureworks database as needed.
Use adventureworks;
Go
DBCC checkident ("HumanResources. Employee ");
Go
B. report the current ID value
The following example reports the current ID value in the employee table of the adventureworks database. If the id value is incorrect, it is not corrected.
Use adventureworks;
Go
DBCC checkident ("HumanResources. Employee", noreseed );
Go
C. Force the current ID value to be set to a new value.
In the following example, the current ID value in the employeeid column of the employee table is set to 300. Because the table has existing rows, the next insert row uses 301 as the value, that is, the current ID value plus 1 (the current added value defined for the column ).
Use adventureworks;
Go
DBCC checkident ("HumanResources. Employee", reseed, 300 );
Go

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.