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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:19:00+00:00 2026-06-08T12:19:00+00:00

We have an ASP.NET/MSSQL based web app which generates orders with sequential order numbers.

  • 0

We have an ASP.NET/MSSQL based web app which generates orders with sequential order numbers.

When a user saves a form, a new order is created as follows:

  1. SELECT MAX(order_number) FROM order_table, call this max_order_number
  2. set new_order_number = max_order_number + 1
  3. INSERT a new order record, with this new_order_number (it’s just a field in the order record, not a database key)

If I enclose the above 3 steps in single transaction, will it avoid duplicate order numbers from being created, if two customers save a new order at the same time? (And let’s say the system is eventually on a web farm with multiple IIS servers and one MSSQL server).

I want to avoid two customers selecting the same MAX(order_number) due to concurrency somewhere in the system.

What isolation level should be used? 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-06-08T12:19:01+00:00Added an answer on June 8, 2026 at 12:19 pm

    Using an identity is by far the best idea. I create all my tables like this:

    CREATE TABLE mytable (
        mytable_id int identity(1, 1) not null primary key,
        name varchar(50)
    )
    

    The “identity” flag means, “Let SQL Server assign this number for me”. The (1, 1) means that identity numbers should start at 1 and be incremented by 1 each time someone inserts a record into the table. Not Null means that nobody should be allowed to insert a null into this column, and “primary key” means that we should create a clustered index on this column. With this kind of a table, you can then insert your record like this:

    -- We don't need to insert into mytable_id column; SQL Server does it for us!
    INSERT INTO mytable (name) VALUES ('Bob Roberts')
    

    But to answer your literal question, I can give a lesson about how transactions work. It’s certainly possible, although not optimal, to do this:

    -- Begin a transaction - this means everything within this region will be 
    -- executed atomically, meaning that nothing else can interfere.
    BEGIN TRANSACTION
        DECLARE @id bigint
    
        -- Retrieves the maximum order number from the table
        SELECT @id = MAX(order_number) FROM order_table
    
        -- While you are in this transaction, no other queries can change the order table,
        -- so this insert statement is guaranteed to succeed
        INSERT INTO order_table (order_number) VALUES (@id + 1)
    
    -- Committing the transaction releases your lock and allows other programs 
    -- to work on the order table
    COMMIT TRANSACTION
    

    Just keep in mind that declaring your table with an identity primary key column does this all for you automatically.

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

Sidebar

Related Questions

We have MSSQL, some C# web services, some ASP.NET, and some AJAX. We're having
We have a huge ASP.NET web application which needs to be deployed to LIVE
I have a ASP.NET 4.0 application which connects to MSSQL through WCF and then
I have been working on ASP.net MVC3 Web application using MSSQL 2008 R2 for
I have asp.net mvc 4 web application project on my server in IIS Directory.
I have asp.net application. and I have created mobile version of it which is
I have a simple ASP.NET MVC + OpenID + NHibernate app (on top of
We have a dot net 2.0 based C# product which uses Mysql as the
I am making a member based web app in ASP MVC3 and I am
I have an asp.net, c# application using MSSQL 2008 server. At some point, i

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.