When IDENTITY_INSERT is set to OFF, you cannot insert an explicit value into the identity column in table ' #TT '.
I was in SQL Server to write the stored procedure encountered this error, then thought: how the temporary table has a primary key, I also did not set the primary key.
Then I worked with my colleagues to debug, and finally found out. The reason is that I took out the real data table ID (actually the primary key) and put it in the temp table (#TT), initially thinking that the properties of the primary key were taken out.
Later debugging together, found that it is not so,
1. SQL Server automatically sets the primary key for the temporary table, and if you want to insert the data, open the primary key so that you can insert the data. "When Identity_insert is set to OFF, you cannot insert an explicit value into the identity column in table ' #TT ' "
2. One is to take multiple fields from the same table to a temporary table, SQL Server does not set a primary key for the temporary table, you can insert it casually
3. Another way is to take a field from multiple tables (with an int type with ID), SQL Server sets the primary key for the temporary table, and if you do not open the primary key, you will get an error when inserting the data " when IDENTITY_INSERT is set to OFF, you cannot #TT to the table" ' The identity column in ' is inserted into an explicit value '
This is the experience of my colleagues and I.
When IDENTITY_INSERT is set to OFF, you cannot insert an explicit value into the identity column in table ' #TT '. SQL Server Temp Table