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

The Archive Base Latest Questions

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

in my SQL Server 2005 database I have a column RMA_Number with datatype char(10)

  • 0

in my SQL Server 2005 database I have a column RMA_Number with datatype char(10) in table RMA.

The value is an increasing number with the format RMA0002511. What is the fastest way to get the highest number to increment it on inserting?

My first approach was:

SELECT     TOP (1) RMA_Number
FROM         RMA
WHERE     (RMA_generated = 1)
ORDER BY Creation_Date DESC

But this was error-prone because it was somehow possible that a higher RMA_Number has an earlier creation date. As a workaround, sorting by the primary key works:

SELECT     TOP (1) RMA_Number
FROM         RMA
WHERE     (RMA_generated = 1)
ORDER BY idRMA DESC

But maybe this is also a possible source of error.

Logically the best way would be to ORDER BY RMA_Number DESC.

But because I was not sure if this gives always the correct result and thought that sorting a char column could get slow if the number of records increase, I chose to order by the Date column.

So,

  1. is it a good idea to order by a char(10)-column (performance and accuracy)?
  2. would it be better to SELECT MAX( RMA_Number ) FROM RMA to get the highest number(perf. and accuracy)
  3. should I stick on using the primary key to order by if the first two points are wrong or should I use an int column and format the number in the application?

EDIT:

I think I must clarify something that I haven’t mentioned. The RMA_Number is not generated on every insert. So maybe there are many records without a number. Martin uses the primary key to build the number. That would be a problem, because the gaps would be too big.

Thank you in advance.

  • 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-18T09:54:48+00:00Added an answer on May 18, 2026 at 9:54 am

    The fastest and safest (for concurrency) way would be to not store the RMA000... prefix at all.

    Just create an integer identity column and add the prefix on via a computed column.

    create table #RMA
    (
    id int identity(2511,1) primary key,
    RMA_Number as 'RMA' + RIGHT('000000' + CAST(id as varchar(7)),7)
    )
    
    insert into #RMA
    default values
    
    select * from #RMA
    

    Or following the new info that not all records have an RMA_Number you could use this approach for a non blocking, efficient, and concurrency safe solution.

    CREATE TABLE dbo.Sequence(
     val int IDENTITY (2511, 2) /*Seed this at 1 + whatever your current max value is*/
     )
    
    GO
    
    /*Call this procedure to get allocated the next sequence number to use*/     
    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

I have a table in a SQL Server 2005 database with a trigger that
I have a SQL Server 2005 database and I have 4 GB of text
We have a Microsoft SQL Server 2005 database that needs to be converted back
We have a production SQL Server 2005 database server with the production version of
I have a SQL Server 2005 database that is suffering from lock starvation because
We have a SQL Server 2005 database, and currently all our users are connecting
I have a SQL Server 2005 database that I'm trying to access as a
I have a SQL Server 2005 database that is linked to an Oracle database.
I have a 'reference' SQL Server 2005 database that is used as our global
I have an existing SQL Server 2005 database that runs our accounting/inventory application. We

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.