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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:32:55+00:00 2026-05-30T07:32:55+00:00

Be gentle, I’m a SQL newbie. I have a table named autonumber_settings like this:

  • 0

Be gentle, I’m a SQL newbie. I have a table named autonumber_settings like this:

Prefix | AutoNumber
SO     | 112320
CA     | 3542

A whenever a new sales line is created, a stored procedure is called that reads the current autonumber value from the ‘SO’ row, then increments the number, updates that same row, and return the number back from the stored procedure. The stored procedure is below:

ALTER PROCEDURE [dbo].[GetAutoNumber]
(
    @type nvarchar(50) ,
    @out nvarchar(50) = '' OUTPUT
)
as
set nocount on

declare @currentvalue nvarchar(50)
declare @prefix nvarchar(10)

if exists (select * from autonumber_settings where lower(autonumber_type) = lower(@type))
begin
    select @prefix = isnull(autonumber_prefix,''),@currentvalue=autonumber_currentvalue 
    from autonumber_settings
    where lower(autonumber_type) = lower(@type)

    set @currentvalue = @currentvalue + 1

    update dbo.autonumber_settings set autonumber_currentvalue = @currentvalue where lower(autonumber_type) = lower(@type)
    set @out = cast(@prefix as nvarchar(10)) + cast(@currentvalue as nvarchar(50))
    select @out as value
end
else
    select '' as value

Now, there is another procedure that accesses the same table that duplicates orders, copying both the header and the lines. On occasion, the duplication results in duplicate line numbers. Here is a piece of that procedure:

BEGIN TRAN

IF exists
(
       SELECT *
       FROM autonumber_settings
       WHERE autonumber_type = 'SalesOrderDetail'
)
BEGIN
       SELECT
                @prefix = ISNULL(autonumber_prefix,'')
               ,@current_value=CAST (autonumber_currentvalue AS INTEGER)
       FROM autonumber_settings
       WHERE autonumber_type = 'SalesOrderDetail'

       SET @new_auto_number = @current_value + @number_of_lines

       UPDATE dbo.autonumber_settings
       SET autonumber_currentvalue = @new_auto_number
       WHERE autonumber_type = 'SalesOrderDetail'
END
COMMIT TRAN

Any ideas on why the two procedures don’t seem to play well together, occasionally giving the same line numbers created from scratch as lines created by duplication.

  • 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-30T07:32:57+00:00Added an answer on May 30, 2026 at 7:32 am

    This is a race condition or your autonumber assignment. Two executions have the potential to read out the same value before a new one is written back to the database.

    The best way to fix this is to use an identity column and let SQL server handle the autonumber assignments.

    Barring that you could use sp_getapplock to serialize your access to autonumber_settings.

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

Sidebar

Related Questions

This is my first question her so please be gentle: I have followig animation
This is my first post on StackOverflow, so please be gentle... I have some
I'm a newbie to this so be gentle! I'm trying to parse a calendar
I'm reading through A Gentle Introduction to Haskell, and early on it uses this
Disclaimer: This is my first time writing unit tests...be gentle! :) I am trying
Disclaimer: i still suck at writing scripts please be gentle if this is a
I'm just starting with Windsor, so please be gentle :) I have a scenario
Be gentle, as my macrofoo is weak. What I'd like to do is something
Totally new to Drupal so be gentle please! Let's say I have a number
Sorry if this is a stupid noob question please be gentle with me I'm

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.