简体   繁体   English

保存表单时在数据库中分配唯一编号

[英]Assign unique numbers in database when saving the form

I have a form where you can create an order and when you save it, is checking in the database (using oracle) for the last order number and is assigning the next one to the currently saved order. 我有一个可以在其中创建订单的表单,当您保存订单时,它正在数据库中(使用oracle)检查最后一个订单号,并将下一个分配给当前保存的订单。 What I found is that if two users are saving a new order both in the same time or at few seconds apart, because of the connection speed my app is unable to assign different numbers for the newly two created orders. 我发现,如果两个用户同时或相隔几秒钟都在保存一个新订单,由于连接速度快,我的应用无法为新创建的两个订单分配不同的编号。 The problem is that both are checking in the same time the last assigned number and both orders get the same number.. 问题是两个都在同一时间检查最后分配的号码,并且两个订单都获得了相同的号码。

I have some ideas but all of them have advantages and disadvantages.. 我有一些想法,但都有优点和缺点。

  1. To have the system wait a few seconds and check the order number when the user saves the order. 要让系统等待几秒钟并在用户保存订单时检查订单号。 But if both saved in the same time, the check will be done in the same time later and I guess that I will end up with the same problem.. 但是,如果两者都保存在同一时间,则检查将在以后的同一时间进行,我想我最终将遇到相同的问题。

  2. To have the system check the order number (a check is run every time the treeview is refreshed) and see if it's been duplicated and then let the user know via the treeview with some highlight, that it's been duplicated. 要让系统检查订单号(每次刷新树形视图时都会运行检查),查看它是否已复制,然后通过树形视图以突出显示的方式告知用户它已被复制。 But if any documents are assigned to the order before the check, then I will end up with documents having a different number in the name and inside from the order to which is assigned.. 但是,如果在检查之前将任何文件分配给订单,那么我将得到名称和内部与分配订单不同的编号的文件。

  3. To have the system check all order numbers periodically and give one of the duplicates a new order number, but Here is the same problem with the documents as at #2.. And also might cause some performance issue.. 要使系统定期检查所有订单号并为重复项中的一个提供新的订单号,但这与#2处的文档存在相同的问题。这也可能会导致性能问题。

  4. Assigning the order number when a user requests a new order not when he saves the order. 在用户请求新订单时(而不是在保存订单时)分配订单号。 I could have the system do Solution #1 along with this solution and recheck to see if the number is being used within the database and then reassign it a new one. 我可以让系统连同此解决方案一起执行解决方案#1,然后重新检查该编号是否正在数据库中使用,然后重新分配一个新编号。 Once again, if documents get assigned, someone has to go fix those. 再一次,如果分配了文档,则必须有人去修复它们。

  5. One way of possibly stopping the documents from being assigned to duplicates is that the user is only allowed put some of the information and then save it or apply it and it does the recheck of #1, and then if it doesn't find anything, allow the user to add documents. 一种可能阻止将文档分配给重复项的方法是,仅允许用户放入一些信息,然后保存或应用该信息,然后对#1进行重新检查,如果找不到任何内容,允许用户添加文档。 This part of the solution could be applied possibly to any of the above but I don't want to delay the users work while is checking the numbers.. 解决方案的这一部分可能适用于上述任何一种方法,但是我不想在检查数字时延迟用户的工作。

Please if you see any improvements to the ideas above or if you have new ones, let me know. 如果您对以上想法有任何改进或有新想法,请告诉我。 I need to find the best solution and as much as possible not to affect the user's current workflow.. 我需要找到最好的解决方案,并尽可能不影响用户当前的工作流程。

If your Order ID is only a number you can use Oracle Sequence. 如果您的订单ID仅是一个数字,则可以使用Oracle Sequence。

CREATE SEQUENCE order_id;

And before you save the record get a new order number. 在保存记录之前,请获取新的订单号。

SELECT order_id.NEXTVAL FROM DUAL;

See also Oracle/PLSQL: Sequences (Autonumber) 另请参见Oracle / PLSQL:序列(自动编号)

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

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