SET IDENTITY_INSERT (Transact-SQL) allows explicit values to be inserted into the table's identity column

Source: Internet
Author: User
Tags one table

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

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.