Https://docs.microsoft.com/zh-cn/sql/t-sql/statements/set-identity-insert-transact-sql
Identity_insert allows you to manually insert an explicit value into the identity column of the table, onallows, off does not allow
When inserting data, the self-growing column is automatically processed by the system and does not require you to specify a value. The self-growing column allows you to specify a value only when the data is inserted when IDENTITY_INSERT is on
At any time, the Identity_insert property of only one table in a session can be set to ON. If a table has set this property to ON, when a set IDENTITY_INSERT ON statement is issued against another table, SQL Server returns an error message stating that set IDENTITY_INSERT is set to on and that its properties are reported The table that is set to ON.
The Identity_insert of table ' XXX.DBO.AAA ' is already on. Cannot perform SET operation for table ' BBB '.
You can set the previous table to off, and then open the table you want to modify to ON,
SET Identity_insertXxx onGOINSERT intoXXX (ID)SELECTId fromAAAGOSET Identity_insertXxxoffGO----------------------------------------SET Identity_insertYYY onGOINSERT intoYYY (ID)SELECTId fromAAAGOSET Identity_insertYYYoffGO
If the insertion value is greater than the current identity value of the table, SQL Server automatically uses the newly inserted value as the current identity value.
Set IDENTITY_INSERT settings are set at run time, not at parse time.
SET IDENTITY_INSERT table name on
SET IDENTITY_INSERT table name off
Summarize:
1. At any point in each connection session, only one table can be set IDENTITY_INSERT on, and the setting is only valid for the current session;
2. In the insert operation on the identity column, be sure to list this identity column.
SET IDENTITY_INSERT (Transact-SQL) allows explicit values to be inserted into the table's identity column