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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:36:09+00:00 2026-05-31T12:36:09+00:00

I have a table with sequential numbers (think invoice numbers or student IDs). At

  • 0

I have a table with sequential numbers (think invoice numbers or student IDs).

At some point, the user needs to request the previous number (in order to calculate the next number). Once the user knows the current number, they need to generate the next number and add it to the table.

My worry is that two users will be able to erroneously generate two identical numbers due to concurrent access.

I’ve heard of stored procedures, and I know that that might be one solution. Is there a best-practice here, to avoid concurrency issues?

Edit: Here’s what I have so far:

USE [master]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_GetNextOrderNumber]
AS
BEGIN
    BEGIN TRAN

DECLARE @recentYear INT
DECLARE @recentMonth INT
DECLARE @recentSequenceNum INT

-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

    -- get the most recent numbers
SELECT @recentYear = Year, @recentMonth = Month, @recentSequenceNum = OrderSequenceNumber
FROM dbo.OrderNumbers 
WITH (XLOCK)
WHERE Id = (SELECT MAX(Id) FROM dbo.OrderNumbers)

    // increment the numbers
IF (YEAR(getDate()) > IsNull(@recentYear,0))
    BEGIN
        SET @recentYear = YEAR(getDate());
        SET @recentMonth = MONTH(getDate());
        SET @recentSequenceNum = 0;
    END
ELSE
    BEGIN
        IF (MONTH(getDate()) > IsNull(@recentMonth,0))
            BEGIN
                SET @recentMonth = MONTH(getDate());
                SET @recentSequenceNum = 0;
            END
        ELSE
            SET @recentSequenceNum = @recentSequenceNum + 1;
    END 

-- insert the new numbers as a new record   
INSERT INTO dbo.OrderNumbers(Year, Month, OrderSequenceNumber)
VALUES (@recentYear, @recentMonth, @recentSequenceNum)

COMMIT TRAN
END

This seems to work, and gives me the values I want. So far, I have not yet added any locking to prevent concurrent access.

Edit 2: Added WITH(XLOCK) to lock the table until the transaction completes. I’m not going for performance here. As long as I don’t get duplicate entries added, and deadlocks don’t happen, this should work.

  • 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-31T12:36:11+00:00Added an answer on May 31, 2026 at 12:36 pm

    you know that SQL Server does that for you, right? You can you a identity column if you need sequential number or a calculated column if you need to calculate the new value based on another one.

    But, if that doesn’t solve your problem, or if you need to do a complicated calculation to generate your new number that cant be done in a simple insert, I suggest writing a stored procedure that locks the table, gets the last value, generate the new one, inserts it and then unlocks the table.

    Read this link to learn about transaction isolation level

    just make sure to keep the “locking” period as small as possible

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

Sidebar

Related Questions

I have some simple query: SELECT foo, bar FROM table i think you now
I have a table of user-uploaded objects. Each user can have an arbitrary number
Suppose I have a 2 column table (id, flag) and id is sequential. I
I have table with some fields that the value will be 1 0. This
I have table in data base name train delay, with columns train number(int), DelayTime(int),
I have a table with some 30 columns, already used in the application extensively.
I have a derived table that contains number of joins, I am trying to
I have a table in SQL Server 2005 which contains some changelog data for
I have a large number of sequential integers i need to do a lookup
I have a table with sequential timestamps: 2011-03-17 10:31:19 2011-03-17 10:45:49 2011-03-17 10:47:49 ...

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.