DBCC Checkident resets the database identity column starting at a value _mssql

Source: Internet
Author: User
Tags first row
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 to check for its current identity value. The specified table must contain an identity column. The table name must conform to the rules for identifiers.
Noreseed
Specifies that the current identity value should not be changed.
Reseed
Specifies that the current identity value should be changed.
New_reseed_value
The new value used as the current value for the identity column.
With NO_INFOMSGS
Suppresses the display of all informational messages.
Result set
DBCC Checkident Returns the following information (the return value may be different) regardless of whether any options are specified for the table that contains the identity column:
Checking Identity Information:current Identity value ' 290 ', current column value ' 290 '.
DBCC execution completed. If DBCC printed error messages, contact your system administrator. Note
The specific corrections made to the current identity value depend on the parameter specification.
DBCC checkident commands identify corrections or corrections made
DBCC checkident (table_name, noreseed)
The current identity value is not reset. DBCC Checkident Returns the current identity value and the current maximum value of the identity column. If the values are not the same, the identity value should be reset to avoid potential errors or whitespace in the value sequence.
DBCC Checkident (table_name)
Or
DBCC checkident (table_name, reseed)
If the current identity value of the table is less than the maximum identity value stored in the identity column, the maximum value in the identity column is used to reset it.
DBCC checkident (table_name, reseed, New_reseed_value)
Sets the current identity value to New_reseed_value. If no rows have been inserted in the table since the table was created, or if all rows have been deleted using the TRUNCATE table statement, the first row inserted after the DBCC checkident is run will use New_reseed_value as the identity. Otherwise, the next line you insert uses the New_reseed_value + current increment value.
If the table is not empty, setting the identity value to a number that is less than the maximum value in the identity column, one of the following occurs:
If a PRIMARY KEY or a UNIQUE constraint exists in the identity column, error message 2627 is generated when the insert operation is subsequently performed in the table because the generated identity value will conflict with the existing value.
If no PRIMARY KEY or UNIQUE constraint exists, the subsequent insert operation will produce duplicate identity values.
Abnormal
The following table lists the conditions that DBCC checkident does not automatically reset the current identity value and provides a way to reset the value.
Conditional Reset Method
The current identity value is greater than the maximum value in the table.
The DBCC CHECKIDENT (TABLE_NAME, noreseed) can be executed to determine the current maximum value in the column and then specify the value as the DBCC CHECKIDENT (TABLE_NAME, reseed, new_reseed_value) command In the New_reseed_value.
Or
Executes the DBCC CHECKIDENT (TABLE_NAME, RESEED, New_reseed_value) when the new_reseed_value is set to a very low value, and then runs DBCC CHECKIDENT (table_ Name, reseed) to correct the value.
Deletes all rows in the table.
Executes the DBCC checkident (table_name, RESEED, new_reseed_value) with New_reseed_value set to the desired start value.
Change the seed value
The seed value is the value that is inserted into the identity column for the first row of the loaded table. All subsequent rows contain the current identity value and the increment value, where the current identity value is the most recent identity value generated for the current table or view. For more information, see Creating and modifying Identifier columns.
You cannot use DBCC checkident to perform the following tasks:
Changes the original seed value that is specified for the identity column when the table or view is created.
Resets 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 re-create the identity column, and then specify a new seed value for the identity column. When a table contains data, the identification number is also added to an existing row with the specified seed and increment values. Cannot guarantee the order in which the rows are updated.
Permissions
The caller must own the table or be a member of the sysadmin fixed server role, the db_owner fixed database role, or the db_ddladmin fixed database role.
Example
A. Reset the current identity value as needed
The following example resets the current identity value of the Employee table in the AdventureWorks database, as needed.
Use AdventureWorks;
Go
DBCC checkident ("HumanResources.Employee");
Go
B. Reporting current identity values
The following example reports the current identity value in the Employee table of the AdventureWorks database, but does not correct if the identity value is incorrect.
Use AdventureWorks;
Go
DBCC checkident ("HumanResources.Employee", noreseed);
Go
C. Force the current identity value to a new value
The following example forces the current identity value in the EmployeeID column in the Employee table to be set to 300. Because the table has existing rows, the next insertion line uses 301 as the value, which is the current identity 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.