Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5939589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:50:57+00:00 2026-05-22T15:50:57+00:00

Multiply users can call store procedure(SP) , that will make some changes to mytable

  • 0

Multiply users can call store procedure(SP), that will make some changes to mytable in SQL Server. This SP should insert some rows to mytable that has reference to itself through parentid column.

TABLE mytable(
   id int identity(1,1) primary key,
   name varchar(20) not null,
   parentId int not null foreign key references mytable(id)
)  

in order to insert row to such table, accordingly to other posts, I have 2 ways:

  1. Allow null to parentid column by ALTER TABLE mytable alter column parentid int null;, insert the row, update parentid and than disable null to parentid
  2. Allow IDENTITY by set identity_insert maytable on, insert dummy row with id=-1 and parentid=-1, insert the correct row with reference to -1, update the parentid to SCOPE_IDENTITY() and in the end set IDENTITY to off

The case:

Assume I take the 2nd way. SP managed to set identity_insert mytable on BUT didn’t yet finished the execution of the rest SP. At this time, there are other INSERT requests(NOT through SP) to the mytable table like INSERT INTO mytable(name,parentid) VALUES('theateist', -1). No id is specified because they assumed that IDENTITY is off and therefore id is auto-incremental.

The Question:

Will this cause errors while inserting because IDENTITY, in this period of time, is ON and not auto-incremental any more and therefore it will require id specification? If yes, it will be better to use the 1st way, isn’t it?

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:50:58+00:00Added an answer on May 22, 2026 at 3:50 pm

    identity_insert is a per-connection setting – you won’t affect other connections/statements running against this table.

    I definitely wouldn’t suggest going the first way, if it could be avoided, since it could impact other users of the table – e.g. some other connection could do a broken insert (parentid=null) while the column definition allows it, and then your stored proc will break. Also, setting a column not null forces a full table scan to occur, so this won’t work well as the table grows.

    If you did stick with method 2, you’ve still got an issue with what happens if two connections run this stored proc simultaneously – they’ll both want to insert the -1 row, at different times, and delete it also. You’ll have conflicts.

    I’m guessing the problem you’re having is inserting the “roots” of the tree(s), since they have no parent, and so you’re attempting to have them self referencing. I’d instead probably make the roots have a null parentid permanently. If there’s some other key column(s), these could be used in a filtered index or indexed view to ensure that only one root exists for each key.


    Imagine that we’re building some form of family trees, and ignoring most of the realities of such beasts (such as most families requiring children to have two parents):

    CREATE TABLE People (
        PersonID int IDENTITY(1,1) not null,
        Surname varchar(30) not null,
        Forename varchar(30) not null,
        ParentID int null,
        constraint PK_People PRIMARY KEY (PersonID),
        constraint FK_People_Parents FOREIGN KEY (ParentID) references People (PersonID)
    )
    
    CREATE UNIQUE INDEX IX_SoleFamilyRoot ON People (Surname) WHERE (ParentID is null)
    

    This ensures that, within each family (as identified by the surname), exactly one person has a null ParentID. Hopefully, you can modify this example to fit your model.

    On SQL Server 2005 and earlier, you have to use an indexed view instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a server that if i make a call to it will return
I'm building SaaS application where back end will be SQL Server and WCF Services.
In an application where users can belong to multiple groups, I'm currently storing their
What options are there for installing Django such that multiple users (each with an
We frequently have users that create multiple accounts and then end up storing the
I am working on a .NET application that displays multiple charts. My users would
I'm in a situation where I need to make a call to a stored
I can't seem to get an answer through google about this. If I perform
I am working on designing a collaborative system where multiple users can edit the
I've been stuck with this problem for over a week now. Hopefully some one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.