简体   繁体   English

死锁SQL Server使用表的最大值

[英]Deadlock sql server using max of table

I have the following situation in sql server. 我在sql server中有以下情况。 I have two tables ( companies and orders ). 我有两个表( companiesorders )。 Within orders there is the companyid . 在订单中有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. 我问这个问题,因为我正在并行运行多个请求的插入,并且在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. 谢谢。

I believe that several SEQUENCE objects could help you. 我相信几个SEQUENCE对象可以为您提供帮助。

"Unlike identity column values, which are generated when rows are inserted, an application can obtain the next sequence number before inserting the row by calling the NEXT VALUE FOR function." “与插入行时生成的标识列值不同,应用程序可以通过调用NEXT VALUE FOR函数在插入行之前获取下一个序列号。”

But there is a caveat: "The sequence number is allocated when NEXT VALUE FOR is called even if the number is never inserted into a table." 但有一个警告: “即使从未将序号插入表中,当调用NEXT VALUE FOR时,也会分配序号。” This means that you cannot guarantee the consecutiveness of the values - and to be honest I don't think that you can ever guarantee such a feature, not without using complex logic. 这意味着您不能保证值的连续性-老实说,我认为您不能保证这样的功能,而不是不使用复杂的逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM