Insert records that reference the identity column recursively

Source: Internet
Author: User
There is a table in SQL Server. The design is as follows:
Instance No. pi_sequence
Root instance id pi_rootid
Parent instance id pi_fatherid
The instance serial number column is the identity column. Both the root instance id and parent instance id reference the instance serial number column. Three records with identical fields may appear, the previous practice was to use two statements: Insert first and then update the root instance id and parent instance id. For details, see how to get the newly inserted identity column value in C #. today, I tried the following statement at will: insert into tbprocessinstance (process_id, pi_rootid, pi_fatherid) values (1, scope_identity () + 1, scope_identity () + 1)

It can be implemented in one step, but I do not know how the pi_rootid and pi_fatherid inserted in this statement in the transaction environment are null.
This problem is still caused by insufficient basic skills and insufficient understanding of the identity column. Otherwise, you will be able to operate it freely.

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.