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

  • SEARCH
  • Home
  • 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 6929585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:20:12+00:00 2026-05-27T11:20:12+00:00

We have a table with a key field, and another table which contains the

  • 0

We have a table with a key field, and another table which contains the current value of that key sequence, ie, to insert a new record you need to:

UPDATE seq SET key = key + 1
SELECT key FROM seq
INSERT INTO table (id...) VALUES (@key...)

Today I have been investigating collisions, and have found that without using transactions the above code run in parallel induces collisions, however, swapping the UPDATE and SELECT lines does not induce collisions, ie:

SELECT key + 1 FROM seq
UPDATE seq SET key = key + 1
INSERT INTO table (id...) VALUES (@key...)

Can anyone explain why? (I am not interested in better ways to do this, I am going to use transactions, and I cannot change the database design, I am just interested in why we observed what we did.)

I am running the two lines of SQL as a single string using C#’s SqlConnection, SqlCommand and SqlDataAdapter.

  • 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-27T11:20:13+00:00Added an answer on May 27, 2026 at 11:20 am

    First off, your queries do not entirely make sense. Here’s what I presume you are actually doing:

    UPDATE seq SET key = key + 1
    SELECT @key = key FROM seq
    INSERT INTO table (id...) VALUES (@key...)
    

    and

    SELECT @key = key + 1 FROM seq
    UPDATE seq SET key = @key
    INSERT INTO table (id...) VALUES (@key...)
    

    You’re experiencing concurrency issues tied to the Transaction Isolation Level.

    Transaction Isolation Levels represent a compromise between the need for concurrency (i.e. performance) and the need for data quality (i.e. accuracy).

    By default, SQL uses a Read Committed isolation level, which means you can’t get “dirty” reads (reads of data that has been modified by another transaction that but not yet committed to the table). It does not, however, mean that you are immune from other types of concurrency issues.

    In your case, the issue you are having is called a non-repeatable read.

    In your first example, the first line is reading the key value, then updating it. (In order for the UPDATE to set the column to key+1 it must first read the value of key). Then the second line’s SELECT is reading the key value again. In a Read Committed or Read Uncommitted isolation level, it is possible that another transaction meanwhile completes an update to the key field, meaning that line 2 will read it as key+2 instead of the expected key+1.

    Now, with your second example, once the key value has been read and modified and placed in the @key variable, it is not being read again. This prevents the non-repeatable read issue, but you’re still not totally immune from concurrency problems. What can happen in this scenario is a lost update, in which two or more transactions end up trying to update key to the same value, and subsequently inserting duplicate keys to the table.

    To be absolutely certain of having no concurrency problems with this structure as designed, you will need to use locking hints to ensure that all reads and updates to key are serializable (i.e. not concurrent). This will have horrendous performance, but “WITH UPDLOCK,HOLDLOCK” will get you there.

    Your best solution, if you cannot change the database design, is to find someone who can. As Brian Hoover indicated, an auto-incrementing IDENTITY column is the way to do this with superb performance. The way you’re doing it now reduces SQL’s V-8 engine to one that is only allowed to fire on one cylinder.

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

Sidebar

Related Questions

I have a table it includes 3 foreign key field like that: My Table:
I have a table with one field that can point to a foreign key
I have a MySQL database which contains a table of users. The primary key
If I have a table with two foreign key fields to another table, I.E.
I have a table: +----------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key |
I have a table: +--------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key |
I'm trying to write a table trigger which queries another table that is outside
I have a table that has a composite key that consists of two int
I have a table with a primary key (lets call it person), and another
I have 4 tables, which are linked together with a foreign key from another,

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.