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.
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.