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.