I have an invoice table in my database into which new invoices are inserted. Recently we have upgraded our services and have many users inserting invoices into the table. I have written a function that calculates the next invoice number in the sequence (the sequence isn't just sequential numbering).
MAX(CAST(SUBSTRING([InvoiceNumber], 3, LEN([InvoiceNumber]) - 2) AS INT)) FROM [Invoice] WHERE [InvoiceNumber] LIKE @Prefix + '%'
I now want to use this function in an insert statement as follows
INSERT INTO [Invoice]
([InvoicedTo]
,[InvoiceNumber]
,[InvoiceDate]
,[Description]
,[Amount])
VALUES
(@InvoicedTo
,NextInvoiceNumber(@Prefix)
,@InvoiceDate
,@Description
,@Amount)
What I want to make sure that using this insert statement will guarantee that the invoice number cannot be duplicated when multiple users are generating invoices. If it can result in duplicates, I would appreciate suggestions on how to stop any duplicates occuring.
Many Thanks, Paul