1. Suppose I have a table in MS SQL Server database that has an autonumber column (identity column). Let's call the table 'Sales' and identity column 'SaleID'.
2. Then I have a StoredProcedure which inserts the total sale amount, triggering SaleID to generate (cause SaleID is identity) and returns the SaleID generated to the app so it can be used further ahead.
3. Finally, suppose this is all for a web application (ASP.Net) where a lot of users simultaneously can trigger a sale.
If my Stored Procedure code is...
declare @SaleID INT
INSERT INTO dbo.Sales (SaleDate, Total) VALUES (GETDATE(), @HypotheticalSalesTotal)
SELECT @SaleID = MAX(SaleID)
FROM dbo.Sales
...Is this a way to safely obtain the SaleID that corresponds to this sale?
Given the amount of simultaneous buyers at the web app, is there any chance the SaleIDs could cross?
Or does MS SQL Server safely runs StoredProcedures one by one in queue?
Thank you very much!