Link to home
Start Free TrialLog in
Avatar of dynamicrevolutions
dynamicrevolutions

asked on

Reset Identity Incremental No in SQL Server Table

I have a the table which consists of the auto incremental no when each of the new records inserted.

When i delete all of the records from the table and insert a new record, the incremental no will still remain continue. So how do i reset the incremental no to 1 ?
ASKER CERTIFIED SOLUTION
Avatar of Saqib Khan
Saqib Khan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of arbert
arbert

Or use DBCC CHECKIDENT
From books online:

Checks the current identity value for the specified table and, if needed, corrects the identity value.

Syntax
DBCC CHECKIDENT
    ( 'table_name'
        [ , { NORESEED
                | { RESEED [ , new_reseed_value ] }
            }
        ]
    )

Arguments
'table_name'

Is the name of the table for which to check the current identity value. Table names must conform to the rules for identifiers. For more information, see Using Identifiers. The table specified must contain an identity column.

NORESEED

Specifies that the current identity value should not be corrected.

RESEED

Specifies that the current identity value should be corrected.

new_reseed_value

Is the value to use in reseeding the identity column.

Remarks
If necessary, DBCC CHECKIDENT corrects the current identity value for a column. The current identity value is not corrected, however, if the identity column was created with the NOT FOR REPLICATION clause (in either the CREATE TABLE or ALTER TABLE statement).

Invalid identity information can cause error message 2627 when a primary key or unique key constraint exists on the identity column.

The specific corrections made to the current identity value depend on the parameter specifications.

Avatar of imrancs
short to arbert's suggestion

DBCC CHECKIDENT ('MyTable', RESEED, 0)

now it will restart identity from 1.