Identity column concurrency

  • Suppose I have a tableA with primary key has Identity column. In this table multiple user inserts the data simulatenously so because of Indetity column it may create Primary key violation error. If I create Intead of insert trigger to check value is unique then it creates performance issue.

    I would like to know what is the best way to handle this situation so user does not receive primary key violation & still we are able to insert the data?

    Thanks

  • In this table multiple user inserts the data simulatenously so because of Indetity column it may create Primary key violation error.

    What makes you say this?

    Having an identity column as the primary key should not cause duplicates, no matter how many users are simultaneously inserting data into the table.

  • Do not Insert data with IDENTITY_INSERT ON option. Then SQL Server take care of concurrency, it is guranteed that there will not be any duplicate id in Identity column.

  • I was thinking that due to multiple user is entering the data at same time, it may generate primary key violation.

    Thanks

  • Be Cool! You can trust on SQL Server Identity column. It will not give you duplicates.

  • Not if you let SQL manage the identity column. You may get gaps (due to rollbacks), but not duplicates.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Hardy21 (10/21/2010)


    I was thinking that due to multiple user is entering the data at same time, it may generate primary key violation.

    Read up on it in Books Online... you'll feel much better.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply