简体   繁体   中英

How to Reorganize Identity Column Values in SQL Server 2008

I have a SQL Server database set up as the publisher in a replication environment. It has a couple of subscriptions and several different people have been executing INSERT statements on these. Because of the different value ranges allocated for identity columns, we end up with very wide variations once all the data is pushed back up into the publication.

This is of course to be expected and even desirable in a production environment. However, we're still in development and therefore desire to reorganize all identity values so they are sequential. Instead of [1,2,3,1001,1001,1003] we'd like to have [1,2,3,4,5,6] . I realize this means changing the identity column values and to update them accordingly across the relationships they support. Is it possible to do something like this?

Seed an identity column when you create it in Management Studio.

If you create the table using code, you can easily seed the table's identity column using the Transact-SQL (T-SQL) CREATE TABLE statement in the following form:

CREATE TABLE tablename 
(
  columnname datatype identity [(seed, increment)
  [NOT FOR REPLICATION]],
  [columnname ...]
)

In the code, datatype is a numeric column. Both seed and increment are optional and the default value for both is 1.

Figure B shows the result of using CREATE TABLE to create a table named Orders and setting the OrderID column's identity seed and increment values to 100 and 10, respectively. As you can see, the first identity value is 100 and each subsequent value increases by 10. (You can decrease identity values by specify a negative value for increment.)

And Use CREATE TABLE to seed an identity column.

Checking and reseeding

For instance, if you copy all the table's records to an archive table and then delete all the records in the source table, you might want to reseed the source table's identity column, so you can control the sequence. Use T-SQL's DBCC CHECKIDENT as follows to reseed an identity column:

DBCC CHECKIDENT    
(    
  tablename    
  [, [NORESEED | RESEED [, newreseedvalue]]]    
)

[WITH NO_INFOMSGS]

Table A defines this statement's optional parameters.

Table A: DBCC CHECKIDENT

Parameter

Description

NORESEED Returns the current identity value and the current maximum value of the identity column, without reseeding. These values are usually (and should be) the same.

RESEED Changes the current identity value, using the maximum value in the identity column, if the current identity value is less than the maximum identity value stored in the identity column.

newreseedvalue Specifies the new seed value when reseeding. If the table is empty, the first identity value (after executing DBCC CHECKIDENT) will equal newreseedvalue. If the table contains data, the next identity value will equal newreseedvalue + the current increment value (the default is 1). This behavior is new to SQL Server 2005 (and remains in 2008). SQL Server 2000 always increments the seed value.

WITH NO INFOMSGS Suppresses all informational messages.

Technically, DBCC CHECKIDENT checks and corrects an identity value. Simply put, use it to learn the current identity value or to reseed an existing identity column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM