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 6893837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:42:31+00:00 2026-05-27T06:42:31+00:00

I have a process that is inserting data into a database (SQL Server 2008)

  • 0

I have a process that is inserting data into a database (SQL Server 2008) whose schema I cannot modify. The table has an int PK, but no auto-increment. So, I need to get the largest id, increment it and then insert (and return the new id.) This transaction also needs to update a number of other tables at the same time. I’m obviously trying to avoid the race condition of simultaneous inserts.

Begin Transaction (Read Committed)  
    DECLARE @MyVar int;   
    --here be the race condition  
    SET @MyVar = (( SELECT MAX(value) FROM MyTable WITH (ROWLOCK, XLOCK, HOLDLOCK)) + 1);  
    INSERT INTO MyTable ....  
    UPDATE MyOtherTable SET Val = @MyVar WHERE WhatEver  
    SELECT MyRetValName = @MyVar  
    INSERT INTO MyThirdTable ...  
Commit Transaction

Are the transaction isolation level and the table locking hints enough to prevent the race condition or do I need UPDLOCK instead of ROWLOCK? (I have a separate ‘retry’ process if the insert fails.)

  • 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-27T06:42:32+00:00Added an answer on May 27, 2026 at 6:42 am
    SELECT MAX(value) 
    FROM MyTable 
    WITH (XLOCK, HOLDLOCK)
    

    Ought to be sufficient. The HOLDLOCK gives serializable semantics which means a key range lock will be taken on the range at the end of the index backing up the primary key. The XLOCK means that 2 concurrent transactions can’t both acquire this lock simultaneously.

    This does mean that any concurrent callers to your insert procedure will end up being blocked for the duration of the transaction.

    A less blocking solution if you can add a new table would be to create another table with an identity column and insert into that as below.

    CREATE TABLE dbo.Sequence(
     val int IDENTITY (10000, 1) /*Seed this at whatever your current max value is*/
     )
    
    GO
    
    CREATE PROC dbo.GetSequence
    @val AS int OUTPUT
    AS
    BEGIN TRAN
        SAVE TRAN S1
        INSERT INTO dbo.Sequence DEFAULT VALUES
        SET @val=SCOPE_IDENTITY()
        ROLLBACK TRAN S1 /*Rolls back just as far as the save point to prevent the 
                           sequence table filling up. The id allocated won't be reused*/
    COMMIT TRAN
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have an application that takes real time data and inserts it into database.
I am trying to simplify the process of inserting data into my database, so
I'm importing Oracle data into SQL Server. After my OLE DB Source that runs
I have a process that imports a lot of data (950k rows) using inserts
I have two tables that i am inserting records into. I am using PHP
We have customers who, for unassailable reasons, cannot use SQL Server's built-in backup features
I have a nightly SSIS process that exports a TON of data from an
I am in the process of re-writing an MS Access database to SQL server
This is a pretty simple problem. Inserting data into the table normally works fine,
I have Process objects that are monitored from two different views. A Windows.Forms.ListView (actually

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.