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,
- is it a good idea to order by a char(10)-column (performance and accuracy)?
- would it be better to
SELECT MAX( RMA_Number ) FROM RMAto get the highest number(perf. and accuracy) - should I stick on using the primary key to order by if the first two points are wrong or should I use an
intcolumn 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.
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.
Or following the new info that not all records have an
RMA_Numberyou could use this approach for a non blocking, efficient, and concurrency safe solution.