I have the following situation in sql server.
I have two tables (companies
and orders
).
Within orders there is the companyid
.
I need a complex identity or sequence, to identify the next order number relating to the company.
Ex: Companies
ID - Name
1 - Microsoft
2 - Google
Orders
ID - Description - Number - CompanyID
1 - Name - 1 - 1
2 - Name - 2 - 1
3 - Name - 1 - 2
4 - Name - 2 - 2
The problem is the number. It needs to be an incremental according to the company. What is the best solution?
I ask this, because I'm running the insertion of several requests in parallel and I'm having a deadlock in sql server. I request the last Number using Max of Orders, but how is parallel, iam having a deadlock. What would be the best solution?
Thanks.