Sqlsever specifies an explicit value for the ID column

Source: Internet
Author: User

Sqlsever specifies the explicit value for the ID column. Most of them are in two tables with the same structure. One Table inserts data with the specified conditions in the other table, and the following error is reported:
Only when the column list is used and IDENTITY_INSERT is ON can an explicit value be specified for the ID column in the '*' table.
I found a solution on the Internet and tried it myself.
I. The problem is detailed as follows:
System: windows2003
Database: 2005
Database instances: wzgl2004 and wzgl2003. Both instances have the kc table.
Execution statement
Insert into wzgl2004.dbo. kc select * from wzgl2003.dbo. kc
There is an error message:
"Only when the list of columns is used and IDENTITY_INSERT is ON can you specify an explicit value for the ID column in 'wzgl2004. dbo. kc 'of the table. "

Ii. solution:
Copy codeThe Code is as follows:
SET IDENTITY_INSERT test ON
Insert into wzgl2004.dbo. kc (id, name) select (id, name) from wzgl2003.dbo. kc
SET IDENTITY_INSERT test OFF

Iii. SET IDENTITY_INSERT help knowledge
1. SET IDENTITY_INSERT: allows explicit values to be inserted into the table's ID column.
2. Syntax
SET IDENTITY_INSERT [database. [owner.] {table} {ON | OFF}
Parameters
Database
Is the name of the database where the specified table resides.
Owner
Is the name of the table owner.
Table
Is the name of the table containing the ID column.

3. Attaching: at any time, the IDENTITY_INSERT attribute of only one table in the session can be set to ON. If a table has SET this attribute to ON and sends a SET IDENTITY_INSERT ON statement to the other table®SQL Server™An error message is returned, indicating that SET IDENTITY_INSERT has been SET to ON and that this attribute has been SET to ON. If the inserted value is greater than the current table id value, SQL Server automatically uses the new value as the current ID value.
SET IDENTITY_INSERT is SET during execution or running, rather than during analysis.
Sqlsever specifies the explicit value for the identification column to end here. If you have any questions, please contact us at www.cnc.com.cn. China Information Port provides you with an efficient and secure SQL database.

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.