SQL Server method of manually inserting identity columns

Time:2022-6-9

If we insert a value in the identity column, for example:

Copy codeThe codes are as follows:
insert member(id,username) values(10,’admin’)

 

An error message will be returned in the query analyzer:
[plain]
References
Server: Message 544, level 16, status 1, line 1
When identity_ When insert is set to off, explicit values cannot be inserted into the identity column in table’member’.   
In some cases, we need to manually insert the value of the identification column. For example, after deleting some records, the identification column is not continuous, and we want to supplement it. We can make our wishes come true with a switch:

Copy codeThe codes are as follows:
SET IDENTITY_Insert [TableName] ON

Write this in the query analyzer:

Copy codeThe codes are as follows:
SET IDENTITY_Insert member ON  
insert member(id,username) values(1,’admin’)   
SET IDENTITY_Insert member OFF