IDENTITY reset in SQL Server
We will have a look at IDENTITY value reset and explicit insert in database tables.
In certain scenarios you may need to start your IDENTITY column value from a new number. CHECKIDENT command can be used to force the table to restart the auto increment number.
Syntax,
For example, to start employee id from 5000, use the following command.
Insert on IDENTITY Column
In some cases you may need to insert some values to Identity column. Table will throw a warning message if tried to insert a value to auto increment value column. To insert the value in auto number value column, IDENTITY_INSERT should be set to ON and after the insert, it can set it back to OFF.
Syntax,
Example,
In certain scenarios you may need to start your IDENTITY column value from a new number. CHECKIDENT command can be used to force the table to restart the auto increment number.
Syntax,
| Code: |
| DBCC CHECKIDENT (<TableName>, RESEED, <yourReseedValue>) |
For example, to start employee id from 5000, use the following command.
| Code: |
| DBCC CHECKIDENT ("DataBaseName.Employee", RESEED, 5000);
GO |
Insert on IDENTITY Column
In some cases you may need to insert some values to Identity column. Table will throw a warning message if tried to insert a value to auto increment value column. To insert the value in auto number value column, IDENTITY_INSERT should be set to ON and after the insert, it can set it back to OFF.
Syntax,
| Code: |
SET IDENTITY_INSERT <TableName> ON
-- Insert statement
SET IDENTITY_INSERT <TableName> OFF
Example,
| Code: |
SET IDENTITY_INSERT Employee ON |
Commentaires
Enregistrer un commentaire