[Reprinted] only when the column list is used and the identity_insert parameter is on

Source: Internet
Author: User

When identity_insert is set to off, explicit values cannot be inserted into the 'products' column of the table ."

Example:

1. Create a table with an ID column:
Create Table products (ID int identity primary key, product varchar (40 ))

2. Try the following operations in the table:
Insert into products (ID, product) values (3, 'garden shovel ')

The result will cause an error: "When identity_insert is set to off, explicit values cannot be inserted into the ID column in The 'products' table ."

3. Switch:
Set identity_insert products on
Insert into products (ID, product) values (1, 'garden shovel ')

The returned result is correct.

4. Create another table products2 and try the same insert operation:
Create Table products2 (ID int identity primary key, product varchar (40 ))

Then execute:
Set identity_insert products2 on
Insert into products2 (ID, product) values (1, 'garden shovel ')

Cause error: "The identity_insert of the" material. DBO. Products "table is already on. You cannot perform the set operation on the table 'products2 ."

Run the following command:
Set identity_insert products off
Set identity_insert products2 on
Insert into products2 (ID, product) values (2, 'garden shovel ')

Execution passed.

5. Try the following operations:
Set identity_insert products2 on
Insert into products2 select * from products

Cause error: "Only when the column list is used and identity_insert is on can an explicit value be specified for the ID column in 'products2 'of the table ."

6. changed:
Set identity_insert products2 on
Insert into products2 (ID, product) Select * from products

Execution passed.

Summary:

1. At any time in each connection session, only identity_insert on can be set for one table, and the setting is only valid for the current session;
2. When you insert an ID column, make sure to list the ID column (of course, you also need to list other related columns ).

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.