I\'ve got 100 threads that are each calling the stored procedure as defined below.
How do I prevent dirty reads?
SET QUOTED_IDENTIFIER OFF
SET ANSI_NULLS
As already mentioned, you can use the auto increment built-in functionality, such as identity columns or sequence.
If you don't want this, you need to make the access to the table in serial manner: using application lock or other abilities.
For example, you can add the hints to the FIRST access to the table (in the transaction) like the below:
UPDATE CfgCerealNumber
Set CerealNumber = CerealNumber + 1
FROM CfgCerealNumber with (tablockx, holdlock)
WHERE CerealNumberID = @TableID
This will guarantee the sequential access to the table in all parallel threads.