Question

I have a requirement that I need to generates an order number that should be shown to user before giving the order.

I thought to use identity column of database but it is not correct as MSDN says

Be cautious about using IDENT_CURRENT to predict the next generated identity value. The actual generated value may be different from IDENT_CURRENT plus IDENT_INCR because of insertions performed by other sessions.

Should I create another column for order number but here i need to manage the uniqueness of order id.

So what would be the best alternative to do this.

Was it helpful?

Solution 2

At what point do you need to generate the order number? I would usually add the order record to the database and retrieve the ID assigned to that row. This would then be used as the order number, perhaps padding it out to a particular number of digits or adding some alpha characters to the front. For example, a row added with ID 42 might be formatted as the order number "ORD000042".

OTHER TIPS

add a field called "OrderNumber" on the level of customer table and by default set to 0 ( assuming that each customer doesn't has an order yet)

on creating new order you can add the order Id as ( Letter O + Customer Id + OrderNumber +1) and on saving the order increment the OrderNumber in the table customer by 1 for the given customer

example:

table customer(cid,name,...,orderNumber)
table order(oid,cid,total,date,code)

on creating new order => code= string.Format("O{0}-{0:0000}",cid,orderNumber+1)

on saving the order => update customer set orderNumber=orderNumber+1 where cid=*customer id*

this way you can track all the orders per customer for example customer 100 has 3 orders => the new order code will be O100-00004

and the customer 109 which has already 15 orders => the new order code will be O109-00016

and so on

regards

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top