When IDENTITY_INSERT is set to OFF, you cannot insert an explicit value into the identity column in table ' #TT '. SQL Server Temp Table

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.