Yes that is always an issue. My instinct is to avoid letting people create their own account numbers, but if that is an absolute requirement AND if a separately formed set of sequential account numbers are needed, I see two requirements:
1 - Customer can enter any account number manually, except for one that is in the exact format of the sequential series. So for example, if the sequential series is all numbers and is 6 digits long, then the manually entered number must either contain a letter or be 5 digits or shorter. This avoids any collision with automatically-generated numbers.
2 - A table with one row (or one row per automatically generated series, perhaps there could be different schemes involving numbers and letters) that always has the next available account number in it. Then, a stored procedure that returns a number for use:
Code:
create table #nextaccountnumber (a int)
insert #nextaccountnumber select 1 -- first available number is 1
GO
create procedure #getnextnumber @nextaccountnumber int output
as
set nocount on
declare @i int
update #nextaccountnumber set a = a + 1, @nextaccountnumber = a
GO
-- now get the next account number available
declare @num int
exec #getnextnumber @num output
print @num
There is another option which is for the customer to choose an option "<select account number for me>" and then only get the max() value at record insertion time, with appropriate locks to guarantee no one else collides with that number who happens to submit the request at just the right time creating a race condition. But the drawback is that the user won't see the new account number until after the row is inserted.
The drawback of the above method is that numbers get consumed whether or not the user finally presses save to create the account, so some numbers will be left unused for actual accounts.
[COLOR=black #e0e0e0]For SQL and technical ideas, visit my blog,
Squared Thoughts.
The best part about anything that has cheese is the cheese.[/color]