Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
Hi,

I have a table named orders.While inserting to the orders table the order number and date will be inserted .Order number will be created using function createOrderNumber() which will give the next order number from order table(ie. max(order num + 1)).
But while two users inserting same time the select same order number is inserting because select statement executes first and order number is not unique. Any solution for that?
this is my query:
SQL
insert into orders (order_number,order_datetime) select dbo.createOrderNumber(@order_category_str),@order_datetime)
Posted
Updated 2-Aug-12 18:53pm
v3
Comments
Santhosh Kumar Jayaraman 3-Aug-12 0:54am    
Is that column has primary ket constraint?
danilmanuel 3-Aug-12 1:06am    
That column doesnt have any primary key constraint.

 
Share this answer
 
v3
Hi,

try this
SQL
INSERT INTO Orders (order_number,order_datetime)
values (exec ('select (isnull(max(order_number),0)+1) from Orders ),@order_datetime)


Best Luck
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900