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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:48:05+00:00 2026-05-18T05:48:05+00:00

i want to generate a unique number from a table. It has to be

  • 0

i want to generate a unique number from a table.
It has to be thread safe of course, so when i check for the last number and get ‘3’, and then store ‘4’ in the database, i don’t want anybody else just in between those two actions (get the number and store it one higher in the database) also to get ‘3’ back, and then also storing ‘4’

So i thought, put it in a transaction like this:

begin transaction
        declare @maxNum int
        select @maxNum = MAX(SequenceNumber) from invoice
            where YEAR = @year
        if @maxNum is null
        begin
            set @maxNum = 0
        end
        set @maxNum = @maxNum + 1
        INSERT INTO [Invoice]
           ([Year]
           ,[SequenceNumber]
           ,[DateCreated])
     VALUES
           (@year
           ,@maxNum
           ,GETUTCDATE()
)

    commit transaction

    return @maxNum

But i wondered, is that enough, to put it in a transaction?
my first thought was: it locks this sp for usage by other people, but is that correct? how can sql server know what to lock at the first step?

Will this construction guarantee me that nobody else will do the select @maxnum part just when i am updating the @maxnum value, and at that moment receiving the same @maxnum as i did so i’m in trouble.

I hope you understand what i want to accomplish, and also if you know if i did choose the right solution.

EDIT:
also described as ‘How to Single-Thread a stored procedure’

  • 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-18T05:48:05+00:00Added an answer on May 18, 2026 at 5:48 am

    As it turned out, i didn’t want to lock the table, i just wanted to execute the stored procedure one at a time.
    In C# code i would place a lock on another object, and that’s what was discussed here
    http://www.sqlservercentral.com/Forums/Topic357663-8-1.aspx

    So that’s what i used

    declare @Result int
    EXEC @Result =
    sp_getapplock @Resource = 'holdit1', @LockMode = 'Exclusive', @LockTimeout = 10000 --Time to wait for the lock
    IF @Result < 0
    BEGIN
    ROLLBACK TRAN
    RAISERROR('Procedure Already Running for holdit1 - Concurrent execution is not supported.',16,9)
    RETURN(-1)
    END
    

    where ‘holdit1’ is just a name for the lock.
    @result returns 0 or 1 if it succeeds in getting the lock (one of them is when it immediately succeeds, and the other is when you get the lock while waiting)

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

Sidebar

Related Questions

I want to generate some formatted output of data retrieved from an MS-Access database
I have a table that stores transaction information. Each transaction is has a unique
I have a table. The table needs to store a number values about a
I need to generate a unique code from a single PC, my software will
i want to generate a sequence of unique random numbers in the range of
I want to generate 64 bits long int to serve as unique ID's for
I want to generate a unique id which will be used in URLs to
I want to generate unique code numbers (composed of 7 digits exactly). The code
I want to generate a thumbnail preview of videos in Java. I'm mostly JMF
I want to generate some XML in a stored procedure based on data in

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.